Perl Regular Expression - What does gc modifier means?

后端 未结 2 1364
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 02:50

I have a regex which matches some text as:

$text =~ m/$regex/gcxs

Now I want to know what \'gc\' modifier means:

I hav

2条回答
  •  情歌与酒
    2021-02-07 03:04

    In the perldoc perlre http://perldoc.perl.org/perlre.html#Modifiers

    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:

    http://perldoc.perl.org/perlretut.html#Using-regular-expressions-in-Perl

    This URI has 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.

    HTH Lee

提交回复
热议问题