Ok, I understand now, and it seems I spoke too soon: the original code is removing some newlines, since it reduces a sequence of successive newlines to a single one.
I don’t understand how this is working. From perlre#Regular-Expressions:
By default, the "^" character is guaranteed to match only the beginning of the string, the "$" character only the end (or before the newline at the end), and Perl does certain optimizations with the assumption that the string contains only one line. Embedded newlines will not be matched by "^" or "$". You may, however, wish to treat a string as a multi-line buffer, such that the "^" will match after any newline within the string (except if the newline is the last character in the string), and "$" will match before any newline. At the cost of a little more overhead, you can do this by using the /m modifier on the pattern match operator.
— but I don’t see how this explains the behaviour we are seeing?
Update: Ignore this “solution”, it doesn’t remove the whitespace! (Insufficient testing.)
In any case, one way to get the desired behaviour is to add a negative look-ahead assertion:
19:18 >perl -wE "my $s = qq[abc\n\t \ndef\n \n\n\ngh]; $s =~ s/^\s+$ +(?!$)//mg; say $s;" abc def gh 19:19 >
Can someone please explain what the regex is doing here?
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^3: regex doubt on excluding
by Athanasius
in thread regex doubt on excluding
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |