1

Why do these not behave identically?

perl -e '$x = "12aba34ba5"; $, = ", "; print split /[ab]/, $x;'
12, , , 34, , 5

perl -e '$x = "12aba34ba5"; $, = ", "; print split /(a|b)/, $x;'
12, a, , b, , a, 34, b, , a, 5
1
  • You're grouping in the second split. If you'd grouped /[ab]/ to be /([ab])/ you'd get both the same results. Commented Feb 3, 2011 at 10:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.