Custom String Separation in Ruby

I'm trying to find a better way to do this ...

For string

s = "if someBool || x==1 && y!=22314" 

I would like to use Ruby to separate statements from logical operators. Therefore, I would like to divide this into

 ["if","someBool","||","x","==","1","&&","y","!=","22314"] 

I could use s.split (), but it only splits with space as delimiters. but I would like x! = y to be split too (they are valid Boolean sentences, they just don't have a gap between them for good readability). Of course, the easiest way is to require the user to put a space between the Boolean operator and the variables, but is there another way to do this?

+6
split ruby
source share
4 answers

Separate by space or word boundary:

 s = "if someBool || x==1 && y!=22314" a = s.split( /\s+|\b/ ); pa 

Output:

 ["if", "someBool", "||", "x", "==", "1", "&&", "y", "!=", "22314"] 
+4
source share

My rule of thumb: use split if you know what to throw away (delimiters), use regex if you know what to keep. In this case, you know what to store (tokens), therefore:

 s.scan(/ \w+ | (?: \s|\b )(?: \|\| | && | [=!]= )(?: \s|\b ) /x) # => ["if", "someBool", "||", "x", "==", "1", "&&", "y", "!=", "22314"] 

Separators (?: \s|\b ) "should prevent your tokens (e.g. == ) from combining what you don't need (e.g. !== )

+2
source share

You can get split to split everything you want, including regex. Something like:

 s.split( /\s|==|!=/ ) 

... may be the beginning.

Disclaimer: regexen makes my head hurt. I tested it now and it works against your example.


UPDATE: None. split always skips that it splits, so the above code loses == and! = from your example. (Monoceres code works fine.)

But for some reason, if you enclose the delimiter in a regular expression in brackets, it stores the thing in the answer array instead of just splitting it. I donโ€™t know if this is a mistake, a function or some kind of smart construction that I do not appreciate properly.

So in fact you need to:

 s.split( /\s|(==)|(!=)/ ) 

But this hardly explains the code itself. And for everyone I know, this does not work in 1.9.

+1
source share

Something like this works:

 s = "12&&32 || 90==12 !=67" a = s.split(/ |(\|\|)|(&&)|(!=)|(==)/) a.delete("") pa 

For some reason "" remains in the array, the delete line is fixed.

+1
source share

All Articles