22

I have a regex which matches some text as:

$text =~ m/$regex/gcxs

Now I want to know what gc modifier means:

I have searched and found that gc means "Allow continued search after failed /g match".

This is not clear to me. What does continued search means?

As far as I have understood, it means that start matching at the beginning if the /g search fails. But doesn't /g modifier matches the whole string?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
AnonGeek
  • 7,408
  • 11
  • 39
  • 55

2 Answers2

34

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

Sodved
  • 8,428
  • 2
  • 31
  • 43
  • This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Modifiers". – aliteralmind Apr 10 '14 at 00:34
5

In the perldoc perlre, the gc modifiers are discussed:

Global matching, and keep the Current position after failed matching. Unlike i, m, s and x, these two flags affect the way the regex is used rather than the regex itself. See Using regular expressions in Perl in perlretut for further explanation of the g and c modifiers.

The specified ref leads to a page with a sub-section entitled 'Global matching,' which contains a small tutorial/working example, including:

A failed match or changing the target string resets the position. If you don't want the position reset after failure to match, add the //c , as in /regexp/gc . The current position in the string is associated with the string, not the regexp. This means that different strings have different positions and their respective positions can be set or read independently.

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63