G is an anchor; it indicates where the match is forced to start. When G is present, it can't start matching at some arbitrary later point in the string; when G is absent, it can.
It is most useful in parsing a string into discrete parts, where you don't want to skip past other stuff. For instance:
my $string = " a 1 # ";
while () {
if ( $string =~ /Gs+/gc ) {
print "whitespace
";
}
elsif ( $string =~ /G[0-9]+/gc ) {
print "integer
";
}
elsif ( $string =~ /Gw+/gc ) {
print "word
";
}
else {
print "done
";
last;
}
}
Output with G's:
whitespace
word
whitespace
integer
whitespace
done
without:
whitespace
whitespace
whitespace
whitespace
done
Note that I am demonstrating using scalar-context /g matching, but G applies equally to list context /g matching and in fact the above code is trivially modifiable to use that:
my $string = " a 1 # ";
my @matches = $string =~ /G(?:(s+)|([0-9]+)|(w+))/g;
while ( my ($whitespace, $integer, $word) = splice @matches, 0, 3 ) {
if ( defined $whitespace ) {
print "whitespace
";
}
elsif ( defined $integer ) {
print "integer
";
}
elsif ( defined $word ) {
print "word
";
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…