0

I'm using Ruby 2.4. I followed the advice from thsi question -- How to embed regular expressions in other regular expressions in Ruby , but I can't figure out why the expression below isn't matching ...

2.4.0 :021 > TOKENS = ["a", "b"]
 => ["a", "b"]
2.4.0 :022 > str = "B19"
 => "B19"
2.4.0 :023 > str =~ /#{Regexp.union(TOKENS)}/i
 => nil

I've simplified the example so to illustrate my point but I would like to have the reg ex match (as opposed to doing a string.include?)

Community
  • 1
  • 1
Dave
  • 15,639
  • 133
  • 442
  • 830
  • This has already been answered for you before. `Regexp.union(TOKENS)` > `(?-mix:a|b)`. The `/i` you added will not affect the modifier group. TinMan posted a self-explained question later, see the link on top. – Wiktor Stribiżew May 06 '17 at 19:58
  • Hey man, I'm trying the solution in there -- /#{ Regexp.union(%w[a b]) }/ and I'm still getting the non-matching behavior. Exactly what solution were you referring to? – Dave May 06 '17 at 20:07
  • [Here](http://stackoverflow.com/questions/43054542). *"To make the regex case insensitive, pass a case insensitive modifier to the pattern and make sure you use `.source` property of the Regex.union created regex to remove flags"*. – Wiktor Stribiżew May 06 '17 at 20:08
  • Gotcha. Goes to show how much I learned. I didn't even remember asking taht one. – Dave May 06 '17 at 20:12

0 Answers0