有人能给我解释一下下面的表达吗?
$input =~ m/\G".*?"(,|$)/gc || $input =~ m/\G[^,]*(,|$)/gc || die;
发布于 2012-02-29 09:30:16
这里有两个正则表达式。第一个是:
\G # the end of the previous match
".*?" # something in quotes
(,|$) # and a comma, or the end of the string
如果第一个失败,则匹配第二个:
\G # the end of the previous match
[^,]* # anything up to the next comma or end of string
(,|$) # and then a comma, or the end of the string
我的猜测是,这两个正则表达式的设计是为了匹配可以被引用或不被引用的东西,并且可能在逗号后面跟一些更多的项。
( c
修饰符表示如果匹配失败,则保留当前位置,因此如果第一次尝试匹配失败,则\G
锚点在第二次尝试匹配时不会更改。g
修改器为下一次匹配设置\G
的位置。)
发布于 2012-02-29 09:40:10
第一个表示匹配引号后面跟着逗号或字符串末尾的每个字符串(即/gc
)。第二个表示匹配任何包含零个或更多非逗号字符(即*
)的序列(即[^,]
)。请注意,\G
修饰符表示每个新的匹配必须紧跟在前一个匹配之后开始。
https://stackoverflow.com/questions/9496464
复制相似问题