Offhand, I don't see any straightforward way to modify the regex object $c to achieve the result you want. The hard fact is, there is an 'x' in $str at offset 6 that is preceded by zero or more $c.

I think the simplest approach is to use an additional anchor for the match. Since $c consumes everything up to and including the newline, if you begin the match of whatever follows $c with a start-of-(embedded)-line anchor, you get the desired results for the example string given.

perl -wMstrict -le "my $c = qr{ (?> -- [^\n]* (?:\n|\z)) }xms; my $str = qq(a --b x\n x); print qq(match at positions $-[1]-), $+[1]-1 if $str =~ m{ ($c* [ ] x) }xms; " match at positions 2-9 perl -wMstrict -le "my $c = qr{ (?> -- [^\n]* (?:\n|\z)) }xms; my $str = qq(a --b x\n x); print qq(match at positions $-[1]-), $+[1]-1 if $str =~ m{ ($c* x) }xms; " match at positions 6-6 perl -wMstrict -le "my $c = qr{ (?> -- [^\n]* (?:\n|\z)) }xms; my $str = qq(a --b x\n x); print qq(match at positions $-[1]-), $+[1]-1 if $str =~ m{ ($c* ^ [ ] x) }xms; " match at positions 2-9 perl -wMstrict -le "my $c = qr{ (?> -- [^\n]* (?:\n|\z)) }xms; my $str = qq(a --b x\n x); print qq(match at positions $-[1]-), $+[1]-1 if $str =~ m{ ($c* ^ x) }xms; "

(The last example had no output, i.e., no match.)

BTW - These examples were run under Perl 5.8. The 5.10 regex possessive quantifiers might be worth investigating, but I think they are just shorthand for the (?> ... ) 'atomic' construct and would give the same results.


In reply to Re: How do I avoid regex engine bumping along inside an atomic pattern? by AnomalousMonk
in thread How do I avoid regex engine bumping along inside an atomic pattern? by zemane

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.