Perl正则表达式-gc修饰符是什么意思? [英] Perl Regular Expression - What does gc modifier means?
问题描述
我有一个正则表达式,可以将某些文本匹配为:
I have a regex which matches some text as:
$text =~ m/$regex/gcxs
现在,我想知道'gc'修饰符的含义:
Now I want to know what 'gc' modifier means:
我搜索并发现 gc 表示在/g匹配失败后允许继续搜索" .
我不清楚.继续搜索意味着什么?
This is not clear to me. What does continued search means?
据我了解,这意味着如果/g 搜索失败,则从头开始匹配.但是/g 修饰符不匹配整个字符串吗?
As far as I have understood, it means that start matching at the beginning if the /g search fails. But doesn't /g modififier matches the whole string?
推荐答案
/g
修饰符用于记住字符串中的位置",以便您可以递增地处理字符串.例如
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
/c
标志不会在失败的比赛中重置位置.因此,如果我们像这样在第一个正则表达式中添加/c
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 "$&";
}
我们最终得到
abc3de
示例代码: http://ideone.com/cC9wb
这篇关于Perl正则表达式-gc修饰符是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!