My friends and I have been playing around with regular expressions, and we ran into something that we haven't been able to explain.

Here is some test code that shows the "phenomena".

#! G:\Perl\bin\perl.exe $test_string="x"; # # This pattern matches once, and it matches # after the "x", not before. # @test = ($test_string =~ /^x*/g ); $NumMatch=@test; print "Test String\t>$test_string<\n"; print "Prematch\t>$`<\n"; print "Match\t\t>$&<\n"; print "Postmatch\t>$'<\n"; print "Num Matches\t>$NumMatch<\n"; print "Match Arrary:\n"; foreach $m (@test) { print ">$m<\n"; } print "\n"; print "-" x 10; print "\n\n"; # # This pattern matches twice, and # the second match is after the "x" # @test = ($test_string =~ /x*$/g ); $NumMatch=@test; print "Test String\t>$test_string<\n"; print "Prematch\t>$`<\n"; print "Match\t\t>$&<\n"; print "Postmatch\t>$'<\n"; print "Num Matches\t>$NumMatch<\n"; print "Match Arrary:\n"; foreach $m (@test) { print ">$m<\n"; } __END__

The above code produces the following output.

Test String >x< Prematch >x< Match >< Postmatch >< Num Matches >1< Match Arrary: >x< ---------- Test String >x< Prematch >x< Match >< Postmatch >< Num Matches >2< Match Arrary: >x< ><

Here is why we are confused:

Why does the first pattern match after the "x". I understand that "x*" is able to match nothing, but shouldn't it match the nothing before the "x", rather than the nothing after the "x".

The reason I think it should match before the "x" is that I use the "^" assertion. Also, Camel2, p61 says:

"... any regular expression that can match the null string is guaranteed to match at the leftmost position in the string."

Also, why does the second pattern "x*$" match twice when the first pattern matched only once. It seems as though they should either both match once or both match twice.

Thanks,

Carl-Joseph


In reply to ^x* vs x*$ by Carl-Joseph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.