in reply to Re^3: regex doubt on excluding
in thread regex doubt on excluding

Well, the ^ could match the newline after "def", and the $ could match the newline before 'gh'. and all the newlines between those two are greedily accepted by the \s+ and thus eliminated.

^ matching *after* a newline means the first newline would not be included and eliminated. $ matching before a newline means the last newline is not eliminated either.

Those two newlines make for one blank line between the non-blank lines, and any excess whitespace including newlines between them is removed.

Replies are listed 'Best First'.
Re^5: regex doubt on excluding
by Athanasius (Archbishop) on Apr 22, 2014 at 09:33 UTC

    Hello SuicideJunkie, and thanks for the answer. Unfortunately, I’m still confused. :-(

    From your explanation, I would expect that making the whitespace match non-greedy would prevent the intermediate newline(s) from being eliminated. But it doesn’t (see below). Here is my current understanding (obviously flawed) of what should happen:

    • ^ and $ are zero-width assertions, so when they feature in a match the newline they follow/preceed is not substituted. For example:

      18:14 >perl -wE "my $s = qq[\n\n\n]; my $t = $s =~ s{$}{}gmr; say $s e +q $t;" 1 18:14 >
    • \s*? matches zero or more whitespace characters (including newline) non-greedily.

    • With the /g modifier in effect, whenever a match succeeds the regex engine begins looking for the next match one character past where the last successful match began.

    Given these assumptions, I would expect that the regex /^\s*?$/ would match the string "a\n\n\nb" as follows: First, ^ matches after the first newline. Since \s*? is non-greedy, the regex engine looks for the shortest match satisfying \s*?$, and finds it in the zero-length string between the first two newlines. This it replaces with another zero-length string. It then starts looking for the next match with ^ matching after the second newline. Again, it finds and replaces a zero-length string. Finaly, ^ matches after the final newline, but no match is found. Result: the string is unchanged. However:

    What am I missing?

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      What am I missing?

      I think rxrx :) pos , @- and @+

      So it matched the zero length string, doesn't advance position, then matches one newline at same position thus advancing position, then it matches the zero length string again, and thats the end of matches

      "a\n\n\nb" s(2)e(2)pos(2)len(0) ("a\n", "", "\n\nb") s(2)e(3)pos(3)len(1) ("a\n", "\n", "\nb") s(3)e(3)pos(3)len(0) ("a\n\n", "", "\nb")

      I think that makes sense :)

        I’ve finally found some documentation which sheds light on this (and it’s only taken me 4 months!). From perlre#Repeated-Patterns-Matching-a-Zero-length-Substring:

        The higher-level loops preserve an additional state between iterations: whether the last match was zero-length. To break the loop, the following match after a zero-length match is prohibited to have a length of zero.

        I was wrong in thinking that the search position advances after a successful match. It does advance to the position immediately following the last match, but when that match was of zero length the “advance” is zero. But Perl’s regex engine prevents an infinite loop of zero-length matches by applying the rule quoted above.

        Thanks to Anonymous Monk for the useful analysis.

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^5: regex doubt on excluding
by Lotus1 (Vicar) on Apr 21, 2014 at 17:44 UTC

    I just tried jellisii2's solution, s/^\s*$/\n/mg, and it worked without the non-greedy modifier. Each matching line was replaced by a single newline.