in reply to Re: Split and empty strings
in thread Split and empty strings

Thanks Rhandom and davorg,

Looks like I had my problem the wrong way round - split is behaving as I originally thought it ought to - i.e. it *is* matching blank lines perfectly normally - but the regexp is producing more matches, as in the Quick Brown Fox above.

But I can't see (feeling particularly thick today) why the regexp produces those extra matches, even in the simple example above. Could someone elucidate?

Cheers,

andy.

  • Comment on Bother (Re: Re: Split and empty strings)

Replies are listed 'Best First'.
Re: Bother (Re: Re: Split and empty strings)
by davorg (Chancellor) on Apr 10, 2001 at 20:28 UTC

    It's probably a Death to Dot Star! issue. You should read that node and see if you can recast your regex to better define the data that you're trying to match. At the moment, it's matching "string\n\n" as three separate strings when you probably only want two.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Read the article, but it didn't seem to quell the question. Why is it matching twice, and more particularly, how did it match twice at the end of the string when there isn't a newline at the end of the string. And why does a '^' work (see next node) but not the '$'?

      UPDATE
      Nope, it isn't a dot star issue. If anything, it is a '*' issue, but what is the issue?
      #!/usr/bin/perl -w $_ = "The quick\n\nbrown fox\njumped."; print "-------------\n"; foreach (/([^\n]*)/gm){ print "[$_]\n"; } print "-------------\n";
      Gave the following:
      ------------- [The quick] [] [] [brown fox] [] [jumped.] [] -------------
      Same old problem ... new clothes.
        * matches 'zero or more' items. Your results are consistent with that.

        From the start of the string, first the regex encounters 'The quick'. That's zero or more elements followed by a \n. Next, it finds zero or more characters between the end of the previous chunk and the \n -- that is, zero characters. So that's also a match.

        Because there's another \n after the first, there's also a match for the zero non-newlines between the two newlines.

        It continues in that fashion.

        That's the problem with dot-star -- if you ask it to match nothing, it will happily match all nothings, even those you don't normally see.

Re: Bother (Re: Re: Split and empty strings)
by Rhandom (Curate) on Apr 10, 2001 at 20:39 UTC
    This is a little weird. Can somebody explain this.
    #!/usr/bin/perl -w $_ = "The quick\n\nbrown fox\njumped."; print "-------------\n"; foreach (/(.*)$/gm){ print "[$_]\n"; } print "-------------\n"; foreach (/^(.*)/gm){ print "[$_]\n"; } print "-------------\n"; foreach (/^(.*)$/gm){ print "[$_]\n"; } print "-------------\n";
    Produces the following
    ------------- [The quick] [] [] [brown fox] [] [jumped.] [] ------------- [The quick] [] [brown fox] [jumped.] ------------- [The quick] [] [brown fox] [jumped.] -------------
    Even though I found a solution to the problem, I still don't have an explanation for it. Does anybody know what is going on in the engine?

    I've added use re qw(debug); to the script and looked and in the first example it says it matched 0 of 32767 times when the position is still on character 9. On the second set, this doesn't happen. How is this matching twice on the first time. Also I tried this
    #!/usr/bin/perl -w $_ = "The quick\n\nbrown fox\njumped."; print "-------------\n"; foreach (/(?:^|\G)(.*)/gm){ print "[$_]\n"; } print "-------------\n";
    And look what it did
    ------------- [The quick] [] [] [brown fox] [] [jumped.] [] -------------
    Hmmmmmmm.
      Here's my guess (and I could be wrong) of what's happening.

      Your first and last RE match zero-to-many characters from the end of the last match to the end of the line. So, first it matches "The quick" and puts its mark at the end of that. Next, it starts after "The quick"-- at the end of the line-- and looks for zero or more characters. Since the mark is at the end of the line, that matches, so you get the empty match. Zero equals zero. :) Ditto for after "brown fox" and "jumped." '$' matches the end of the string as well as the end of the line.

      Your second pattern forces the match to begin at the beginning of a line, so the secondary matches can't happen.

      I suspect that having the zero-length patterns match once and only once in a global search is a compromise between sanity-- those ARE valid matches, after all-- and insanity. After all, if zero-length patterns matched more than once in a global search, any zero-length pattern would turn a global match into an infinite loop. A zero-length match can't, by definition, advance the mark, so it would also match the next time, and the next, etc.

      stephen

        Indeed your suspicion is correct. Here is the relevant (long) section of documentation from 5.005, I suspect it is still roughly similar in 5.6. Anyone who wants to check and find a better way to write this is free to submit a patch.
        Repeated patterns matching zero-length substring WARNING: Difficult material (and prose) ahead. This section needs a rewrite. Regular expressions provide a terse and powerful programming language. As with most other power tools, power comes together with the ability to wreak havoc. A common abuse of this power stems from the ability to make infinite loops using regular expressions, with something as innocuous as: 'foo' =~ m{ ( o? )* }x; The o? can match at the beginning of 'foo', and since the position in the string is not moved by the match, o? would match again and again due to the * modifier. Another common way to create a similar cycle is with the looping modifier //g: @matches = ( 'foo' =~ m{ o? }xg ); or print "match: <$&>\n" while 'foo' =~ m{ o? }xg; or the loop implied by split(). However, long experience has shown that many programming tasks may be significantly simplified by using repeated subexpressions which may match zero-length substrings, with a simple example being: @chars = split //, $string; # // is not magic in +split ($whitewashed = $string) =~ s/()/ /g; # parens avoid magic +s// / Thus Perl allows the /()/ construct, which forcefully breaks the infinite loop. The rules for this are different for lower-level loops given by the greedy modifiers *+{}, and for higher-level ones like the /g modifier or split() operator. The lower-level loops are interrupted when it is detected that a repeated expression did match a zero-length substring, thus m{ (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x; is made equivalent to m{ (?: NON_ZERO_LENGTH )* | (?: ZERO_LENGTH )? }x; 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. This prohibition interacts with backtracking (see the section on Backtracking), and so the second best match is chosen if the best match is of zero length. Say, $_ = 'bar'; s/\w??/<$&>/g; results in "<<b><><a><><r><>">. At each position of the string the best match given by non-greedy ?? is the zero- length match, and the second best match is what is matched by \w. Thus zero-length matches alternate with one- character-long matches. Similarly, for repeated m/()/g the second-best match is the match at the position one notch further in the string. The additional state of being matched with zero-length is associated to the matched string, and is reset by each assignment to pos().
        This is probably my least favorite piece of magic in the regular expression engine, but there you have it. If ever you seek to write a custom parse engine it is important to check whether you need to write:
        pos($string) = pos($string); # Resets an internal flag
        from time to time.