Sorry, but the code shown is not removing newlines, as is easily demonstrated:
18:23 >perl -wE "my $s = qq[abc\n\t \ndef\n \ngh]; $s =~ s/^\s*$/X/m +g; say $s;" abc X def X gh 18:24 >
That’s because the /m modifier lets ^ and $ match at the beginning and end of each line within the string (see perlre#Modifiers). What the code does is to remove any whitespace from an otherwise empty line, i.e. whitespace is removed if and only if the whitespace is the only thing between two newlines (or between the beginning of the string and the first newline, or between the last newline and the end of the string). Is this what was intended? Or were you wanting to remove all whitespace except newlines themselves? If the latter, Laurent_R’s approach is what you want:
18:24 >perl -wE "my $s = qq[abc\n\t \nd\tef\n \ngh]; $s =~ s/[ \t]+/ +X/mg; say $s;" abc X dXef X gh 18:39 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: regex doubt on excluding
by Athanasius
in thread regex doubt on excluding
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |