\G
in a pattern?
\G
is used with the /g
modifier (and ignored elsewhere)in a match or substitution, and sets an
anchor to just past where the last match occurred, i.e. the
pos
point.
For example, suppose you had line of text quoted in standard mail and
Usenet notation, (that is, with leading >
characters), and you want change each leading >
into a corresponding :
. You could do so in this way:
s/^(>+)/':' x length($1)/gem;
Or, using \G
, the much simpler (and faster):
s/\G>/:/g;
A more sophisticated use might involve a tokenizer. The following example, (courtesy of Jeffrey Friedl) did not work in 5.003 due to bugs, but does work in 5.004 or better:
while (<>) { chomp; PARSER: { m/ \G( \d+\b )/gx && do { print "number: $1\n"; redo; }; m/ \G( \w+ )/gx && do { print "word: $1\n"; redo; }; m/ \G( \s+ )/gx && do { print "space: $1\n"; redo; }; m/ \G( [^\w\d]+ )/gx && do { print "other: $1\n"; redo; }; } }
Of course, that could have been written as
while (<>) { chomp; PARSER: { if ( /\G( \d+\b )/gx { print "number: $1\n"; redo PARSER; } if ( /\G( \w+ )/gx { print "word: $1\n"; redo PARSER; } if ( /\G( \s+ )/gx { print "space: $1\n"; redo PARSER; } if ( /\G( [^\w\d]+ )/gx { print "other: $1\n"; redo PARSER; } } }
But then you lose the vertical alignment of the patterns.