ruby - How do I split on multiple white space or tabs?



Hey in Ruby how do you split on multiple white space or a tab character? I tried this

2.4.0 :003 > a = "b\tc\td"
 => "b\tc\td" 
2.4.0 :005 > a.strip.split(/([[:space:]][[:space:]]+|\t)/)
 => ["b", "\t", "c", "\t", "d"]

but the tabs themselves are getting turned into tokens and that's not what I want. The above should return

["b", "c", "d"]

2 Answers: 

It happens because the group you used is a capturing one. See split reference:

If pattern contains groups, the respective matches will be returned in the array as well.

Use a non-capturing group (used only for grouping patterns) to avoid adding matched strings into the resulting array:


In this instance you can use a character class that includes both spaces and tabs in your regular expression:

"b\tc\td".split /[ \t]+/

If you want to split on any whitespace, you can also use the [\s]+ notation, which matches all whitespace characters.