I'm using Rails 5. I want to replace occurrences in a string of an arbitrary amount of numbers, an optional number of spaces, a token from an array of strings, an optinal number of spaces, and an arbitrary amount of numbers. So for example, this would match my pattern ....
123 / 2221111
or this
102849/222
so I thought my regular expression should be
re = /\d+[[:space:]]*[#{Regexp.union(TOKENS)}][[:space:]]*\d+/
but the following is matching something that does not fit the criteria ...
2.4.0 :015 > re = /\d+[[:space:]]*[#{Regexp.union(TOKENS)}][[:space:]]*\d+/
=> /\d+[[:space:]]*[(?-mix:of|\/)][[:space:]]*\d+/
2.4.0 :016 > TOKENS
=> ["of", "/"]
2.4.0 :017 > name = "10F 2017"
=> "10F 2017"
2.4.0 :018 > name.gsub!(re, '#')
=> "#"
The string "10F 2017" does not contain any of the strings from my TOKENS array so right there things should fail. How do I rewrite my regex so that it only matches what I want?


[(?-mix:of|\/)]matches a single char:(, a range from?tom(i.e. a lot of letters, all ASCII uppercase ones),i. etc. Delete the[and].