good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: regex doubt on excludingby kcott (Archbishop) |
on Apr 22, 2014 at 02:31 UTC ( [id://1083100]=note: print w/replies, xml ) | Need Help?? |
If you look in "perlre: Metacharacters", you'll see that '$' matches "the end of the line (or before newline at the end)" (emphasis added). So, what you really want is to match any whitespace character except if it follows '$'. That makes your substitution "s/(?<!$)\s//gm", shown here:
Output:
However, if you know that you only want to match the whitespace characters space (" ") and tab ("\t"), then the transliteration "y/\t //d" will be faster. (See "Search and replace or tr" in "Perl Performance and Optimization Techniques" for a Benchmark example.) As you can see, the code is virtually identical (which makes replacing the s/// with y/// easy):
Output:
[In case you didn't know, y/// and tr/// are synonymous. You'll find both forms used in different sections of the perlop documentation.] -- Ken
In Section
Seekers of Perl Wisdom
|
|