... what would be the scope of \G. Supposes in next print statement I use a different string, then also \G would will make pattern match to start from a position where it last matched in string one?

Each individual string has an independent "position of end of last successful match" attribute that is returned by the pos built-in. The  \G regex operator (enabled by the  /g modifier) accesses this attribute of a string being matched to assert that matching in that string is continuing where previous matching in that string by any  m//g match left off.

c:\@Work\Perl\monks>perl -wMstrict -le "my $s1 = 'foobarfeefiefoefum'; $s1 =~ /foo/g; ;; my $s2 = '123456789'; $s2 =~ /6/g; ;; print qq{A: pos in \$s1 '$s1' after successful match == }, pos $s1; print qq{B: pos in \$s2 '$s2' after successful match == }, pos $s2; ;; $s1 =~ /foe/g; print qq{C: pos in \$s1 '$s1' after successful match == }, pos $s1; print qq{D: pos in \$s2 '$s2' still == }, pos $s2; " A: pos in $s1 'foobarfeefiefoefum' after successful match == 3 B: pos in $s2 '123456789' after successful match == 6 C: pos in $s1 'foobarfeefiefoefum' after successful match == 15 D: pos in $s2 '123456789' still == 6

What would have happened if the second match against the  $s1 string had been  /\Gfoe/g instead? Or  /\Gbar/g instead? Try it and see! (See also the documentation concerning the effect of the  /c modifier in conjunction with  /g in a  m//gc match.)

(Incidentally, what if  $test_string in your example code was  '12xxx345' and the match was  /(\d)/g (no \G) instead? What if it was  /\G(\d)/g as originally?)


Give a man a fish:  <%-{-{-{-<


In reply to Re^7: Regular expression by AnomalousMonk
in thread Regular expression by pravakta

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.