开发者

Splitting a string using a regex in Perl

开发者 https://www.devze.com 2023-02-08 02:27 出处:网络
Why do these not behave identically? perl -e \'$x = \"12aba34ba5\"; $, = \", \"; pri开发者_JAVA百科nt split /[ab]/, $x;\'

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

精彩评论

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

关注公众号