开发者

Splitting a string using a regex in Perl

Why do these not behave identically?

perl -e '$x = "12aba34ba5"; $, = ", "; pri开发者_JAVA百科nt split /[ab]/, $x;'
12, , , 34, , 5

perl -e '$x = "12aba34ba5"; $, = ", "; print split /(a|b)/, $x;'
12, a, , b, , a, 34, b, , a, 5


This is documented in perldoc split:

If the PATTERN contains parentheses, additional list elements are created from each matching substring in the delimiter.

You can use (?:a|b) if you don't want to make backreferences.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜