in reply to Split and empty strings

Never underestimate the importance of trying a small test script. Notice what the following code reveals.
#!/usr/bin/perl -w $_ = "The quick\n\nbrown fox\njumped."; print "-------------\n"; foreach (/(.*)/gm){ print "[$_]\n"; } print "-------------\n"; foreach (split/\n/){ print "[$_]\n"; } print "-------------\n";
This code prints out the following:
------------- [The quick] [] [] [brown fox] [] [jumped.] [] ------------- [The quick] [] [brown fox] [jumped.] -------------
So, while it may look the same, it really isn't.

Replies are listed 'Best First'.
Bother (Re: Re: Split and empty strings)
by andye (Curate) on Apr 10, 2001 at 20:03 UTC
    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.

      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.
      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