Problem when splitting string with regex

Hi guys, I am facing a problem when I try to split a string with a regex. Basically I would like to split everytime the regex meet: “ && “, “ || “ or “if “.

I did this example but the result is not as expected:

string = "if adad = 2 && adada = 3 || 2 = 3"
array = string.split(/((if\s+)|(\s+\|\|\s+)|(\s+\&\&\s+))/)

puts array.inspect

The result I got:

["", "if ", "if ", "adad = 2", " && ", " && ", "adada = 3", " || ", " || ", "2 = 3"]

The result I would like:

["adad = 2", "adada = 3", "2 = 3"]

I have a second question too: is it possible to split a string without deleting the chaine that split ?

string = "if adad = 2 && adada = 3 || 2 = 3"
p string.split(/\s*(if|&&|\|\|)\s*/)
p string.split(/\s*(?:if|&&|\|\|)\s*/)

=>

["", "if", "adad = 2", "&&", "adada = 3", "||", "2 = 3"]
["", "adad = 2", "adada = 3", "2 = 3"]

The documentation of String#split should mention that if the regexp has a capturing group, it is included in the result.

1 Like

Ah this is why okay … I understand better now. Thank you so much !

Just one question. What is the purpose of ?: in a regex ?

To match but not capture. Here it allows to have alternatives to the match (id, ||, &&) but to not capture them.

#split have a remove_empty option:

string = "if adad = 2 && adada = 3 || 2 = 3"
p string.split(/\s*(?:if|&&|\|\|)\s*/, remove_empty: true) 
# => ["adad = 2", "adada = 3", "2 = 3"]

Alternatively, you can keep your regex short and remove the spaces after the split:

string = "if adad = 2 && adada = 3 || 2 = 3"
p string.split(/if|&&|\|\|/, remove_empty: true).map &.strip 
# => ["adad = 2", "adada = 3", "2 = 3"]
1 Like

Oh nice, I will need it definitely. Thanks a lot !