in reply to regex doubt on excluding
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:
#!/usr/bin/env perl -l use strict; use warnings; my $string = "\t is a TAB\n is a SPACE\nTAB\t and SPACE"; print '*** BEFORE ***'; print $string; $string =~ s/(?<!$)\s//gm; print '*** AFTER ***'; print $string;
Output:
*** BEFORE *** is a TAB is a SPACE TAB and SPACE *** AFTER *** isaTAB isaSPACE TABandSPACE
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):
#!/usr/bin/env perl -l use strict; use warnings; my $string = "\t is a TAB\n is a SPACE\nTAB\t and SPACE"; print '*** BEFORE ***'; print $string; $string =~ y/\t //d; print '*** AFTER ***'; print $string;
Output:
*** BEFORE *** is a TAB is a SPACE TAB and SPACE *** AFTER *** isaTAB isaSPACE TABandSPACE
[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
|
|---|