I've been using the Percentage Strings Literal to convert strings like "one two three four\ five"
into an array.
%w(one two three four\ five)
returns:
["one", "two", "three", "four five"]
Now I want to do this dynamically, so can use Literals anymore.
Which regex pattern can I use to convert my string above into an array?
I'm looking for a regex pattern to place into a ruby split method that will take "one two three four\ five"
and return ["one", "two", "three", "four five"]
.
Note: I only want to split by whitespaces that aren't escaped, like above. Four and Five were combined into the same string because the whitespace that separated them was escaped.
If your strings have no escape sequences, you may use a splitting approach with
.split(/(?<!\\)\s+/)
Here, (?<!\\)\s+
matches 1+ whitespaces (\s+
) that are not preceded with \
.
If your strings may contain escape sequences, a matching approach is preferable as it is more reliable:
.scan(/(?:[^\\\s]|\\.)+/)
See the Ruby demo.
It will match 1 or more characters other than \
and whitespace (with [^\\\s]
) and any escape sequence (matched with \\.
, a backslash + any char other than line break chars).
To get rid of \
symbols, you will have to use a gsub
later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With