I have a regex which matches some text as:
$text =~ m/$regex/gcxs
Now I want to know what \'gc\' modifier means:
I hav
The /g
modifier is used to remember the "position in a string" so you can incrementally process a string. e.g.
my $txt = "abc3de";
while( $txt =~ /\G[a-z]/g )
{
print "$&";
}
while( $txt =~ /\G./g )
{
print "$&";
}
Because the position is reset on a failed match, the above will output
abcabc3de
The /c
flag does not reset the position on a failed match. So if we add /c
to the first regex like so
my $txt = "abc3de";
while( $txt =~ /\G[a-z]/gc )
{
print "$&";
}
while( $txt =~ /\G./g )
{
print "$&";
}
We end up with
abc3de
Sample code: http://ideone.com/cC9wb