
japhy
Enthusiast
Jan 29, 2000, 2:32 PM
Post #2 of 2
(306 views)
|
Well, the s/ *// regex will remove the FIRST and LONGEST occurrance of 0 or more spaces. Let me show you something that will boggle your mind: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> $string = "fred xxxxxx barney"; $string =~ s/x*//; print $string; # "fred xxxxxx barney" </pre><HR></BLOCKQUOTE> What?! Why didn't it remove the x's in the middle? Well, because the first occurrance of 0 or more x's is right at the beginning of the string. Can't you see the 0 x's before the "f" of "fred"? So it removes those x's, all 0 of them. What you probably want is s/ +//. Or even better, s/ +//g. The + means 1 or more, so that won't match 0 spaces. And the /g means do this for all sets of 1 or more spaces. Even better is the use of tr///. tr/ //d will remove all spaces, and do so quickly. Read the perlre documentation for information on s/// and tr///.
|