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.
精彩评论