I am trying to figure out the best way to do this...
Given a string
s = "if someBool || x==1 && y!=22314"
I'd like to use Ruby to seperate statements and boolean operators.. so I'd like to split this into
["if","someBool","||","x","==","1","&&","y","!=","22314"]
I could use s.split(), but this only splits with space as delimeters..but I'd like x!=y to be split too (they are valid boolean sentences, they just dont have space in between for good readability). Of course the easiest way is to require the user to put space between boolean operator and variables, but are there any other way to do this?
Split on whitespace or a word boundary:
s = "if someBool || x==1 && y!=22314"
a = s.split( /\s+|\b/ );
p a
Output:
["if", "someBool", "||", "x", "==", "1", "&&", "y", "!=", "22314"]
My rule of thumb: use split
if you know what to throw away (the delimiters), use a regex if you know what to keep. In this case you know what to keep (the tokens), so:
s.scan(/ \w+ | (?: \s|\b )(?: \|\| | && | [=!]= )(?: \s|\b ) /x)
# => ["if", "someBool", "||", "x", "==", "1", "&&", "y", "!=", "22314"]
The (?: \s|\b )
"delimiters" are to prevent your tokens (e.g. ==
) from matching something you don't want (e.g. !==
)
Something like this works:
s = "12&&32 || 90==12 !=67"
a = s.split(/ |(\|\|)|(&&)|(!=)|(==)/)
a.delete("")
p a
For some reason "" remained in the array, the delete line fixed that.
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