I assume then, should the data I'm parsing with the regex be well formatted, then pos() will never be reset and so the /c modifier is not needed?

Note that the condition on the while loop is the regex, so the loop will run while the match is true, so the last match executed will always be a failed one. The question then is why it failed, because it reached the end of string (= successful overall parse) or it did not, which is why I compare pos to length - but for pos to be available there, I need /c.

use warnings; use strict; my $string = " foo bar quz "; print "String: '$string' length: ",length($string),"\n"; print "### Successful match with /c:\n"; pos($string)=undef; print "<$1>\n" while $string =~ / \G \s* (foo|bar|quz) \s* /xgc; print "pos: ",pos($string)//'undef',"\n"; print "### Failed match with /c:\n"; pos($string)=undef; print "<$1>\n" while $string =~ / \G \s* (foo|bar) \s* /xgc; print "pos: ",pos($string)//'undef',"\n"; print "### Successful match without /c:\n"; pos($string)=undef; print "<$1>\n" while $string =~ / \G \s* (foo|bar|quz) \s* /xg; print "pos: ",pos($string)//'undef',"\n"; print "### Failed match without /c:\n"; pos($string)=undef; print "<$1>\n" while $string =~ / \G \s* (foo|bar) \s* /xg; print "pos: ",pos($string)//'undef',"\n"; __END__ String: ' foo bar quz ' length: 13 ### Successful match with /c: <foo> <bar> <quz> pos: 13 ### Failed match with /c: <foo> <bar> pos: 9 ### Successful match without /c: <foo> <bar> <quz> pos: undef ### Failed match without /c: <foo> <bar> pos: undef

Another use for /c is described in "\G assertion" in perlop (under "Regexp Quote-Like Operators"): basically, attempting to apply multiple different regexes at the same point in a string until you find one that matches.


In reply to Re^3: Modifying pos() from within (?{...}) by haukex
in thread Modifying pos() from within (?{...}) by mxb

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.