Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attn : Regx gurus

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi all!

I am trying to delete leading and trailing spaces from a string . I used the following substution .

$t=" test test ";

$t=~s/^\s+//;

$t=~s/\s$+//;

print $t;

It works fine . when i try to make replacement in one line code

say

$t=~s/^\s+|\s$+//;

it just replaces leading space not trailing space ..what am i doing wrong.

~Thendal

Morpheus: Have you ever had a dream, Neo, that you were so sure was real? What if you were unable to wake from that dream? How would you know the difference between the dream world and the real world?
 
$t =~ s/^\s*(.*?)\s*$/$1/;
From Programming Perl - O'Reilly

HTH
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
You better do it in two regexes like you have. It's more efficient. I'm surprised that O'Reilly gives a regex like Paul gave. It has to read the entire string whereas with the two regex method it only looks at the beginning and then at the end. To make it all in one line you can do like:

s/^\s+//, s/\s+$// for $var; # for one variable

s/^\s+//, s/\s+$// for @array; # for entire array

s/^\s+//, s/\s+$// for ($var1, $var2, @array); # for multiple variable

I'm sure Paul will be happy to benchmark these if you ask nicely. :)
 
Thanks Paul and TopMach .

Topmach, I tried with s/^\s+//,s/\s+$// as one line but it removed the leading space alone ...


This what i did

$t=" test test ";

print $t;

$t=~ s/^\s+//, s/\s+$//;
print "\n";

print '|'."$t".'|';


Thanks again

~Thendal

Morpheus: Have you ever had a dream, Neo, that you were so sure was real? What if you were unable to wake from that dream? How would you know the difference between the dream world and the real world?
 
thendal,

I would have expected it not to run, but there ye go.

I think TopMach meant you to use it thusly
Code:
$t=" test test   ";
print $t;
s/^\s+//, s/\s+$// for $t;
print "\n";
print '|'."$t".'|';

HTH Paul

Cheers TM

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Thanks Paul, I forget how explicitely I have to give sample code somtimes.
 
I think that thendall initial attempts were more on the mark. One regex was fine for this common operation. He just needed the /g modifier.

$var =~ s/^\s+|\s+$//g;

--jim
 
Thank you all it really helped me .. I am one of those of people who thought compact codes are more efficient ...

sorry Topmach i took your code literally...

Just now entered into the world of regular experssion.

got another question...

if i want to replace a newline character from a string what should i do ...

i tried like these

$t='test
test';

$t=~s/\n/'***'/g;

it works fine on my local machine(win 2000)..but i load same pattern matching in the server ..its not replacing the newline character ....

string in server looks like

test
***test

am i doing something wrong ..(server running on sun solaris )

...

Regards,
~Thendal.


 
try
Code:
$t=~s/\n|\r/'***'/g;

I think its because on a *nix box there's \r's as well as \n's

I put the pipe in there because I'm not sure which comes first, but it means you should now get ****** as the replacement. If it doesn't work you can play with s/\r\n/'***'/g; or s/\n\r/'***'/g;

Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
For clarity's sake...

unix: \n
win: \r\n
mac: \r

thendal, I don't know why it didn't replace the newlines on the remote server but I do know that the code s/\n/'***'/g will replace any newlines to '***'. If it didn't substitute it's because of something else. A perl program should be oblivious to different architectures' newlines as perl itself will do the translation for the appropiate platform. Any ordinary text editor will never show you the hex code of the newlines, but merely the newline itself. To view a true representation of a newline open your script in a hex editor.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top