in reply to regex doubt on excluding
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, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regex doubt on excluding
by Anonymous Monk on Apr 21, 2014 at 08:47 UTC | |
by Athanasius (Archbishop) on Apr 21, 2014 at 09:28 UTC | |
by SuicideJunkie (Vicar) on Apr 21, 2014 at 14:49 UTC | |
by Athanasius (Archbishop) on Apr 22, 2014 at 09:33 UTC | |
by Anonymous Monk on Apr 22, 2014 at 10:58 UTC | |
| |
by Lotus1 (Vicar) on Apr 21, 2014 at 17:44 UTC | |
by Anonymous Monk on Apr 21, 2014 at 09:47 UTC |