in reply to Re: Re: Capturing brackets within a repeat group
in thread Capturing brackets within a repeat group

although the \G in your version isn't doing anything in this case. I believe (but am open to correction) that \G doesn't have any effect unless you also use the /c modifier and even then, it only has an effect once a failure has occurred in which case, a subsequent match on the same target string will start from the point of the previous failure.

then let me correct ;-)

The '\G' forces the next match to start where the last ended. When the regex is executed the first time, '\G' is thus equivalent to '\A' (beginning of string). The next matches (due to the /g modifier) have to start where the previous one ended, so no part of the string can be skipped. This sure makes a difference, see the examples below.

sub test_regex { local $_ = shift; local $\ = "\n"; print 'string: ', $_, ; print 'with \G: ', join(':', m/\G ( [0-9A-Z]{1,2} ) (?: :|$ ) / +igx); print 'without \G: ', join(':', m/ ( [0-9A-Z]{1,2} ) (?: :|$ ) / +igx), "\n"; } test_regex('0:0A:0C:B:B8:F'); test_regex('#0:0A:0C:B:B8:F'); test_regex('0: 0A:0C:B:B8:F'); test_regex('0:0Aa:0C:B:B8:F');

-- Hofmator

Replies are listed 'Best First'.
Re: Re3: Capturing brackets within a repeat group
by BrowserUk (Patriarch) on Jan 12, 2003 at 16:52 UTC

    ...and thankyou for the correction.

    I've only recently begun exploiting the possibilities of \G and /gc, and have found the docs woefully lacking on good examples. Yours is the clearest explanation/ demonstration of the \G assertion I've seen.

    It should be in the manual as far as I'm concerned.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

      Thanks for the praise :)

      But do you know about the new documentation that comes with perl 5.8.0: perlrequick and perlretut?? The part concerning the \G anchor can be found in this section - and I think it's a quite good description.

      -- Hofmator