Jack Jack - 1 year ago 104
Ruby Question

Regexp union with word boundaries

I have a list of patterns and I want to match a string against those patterns but I need to match only entire words, so I was looking for a way to dynamically insert word boundaries into the Regexp.union method but I am missing something.
Here is what I have tried

test_string = "lonewolf is lonely"
pattern_list = ["lonely", "wolf", "jungle"]
pattern_list.collect! { |pattern| pattern = "\b" + pattern + "\b"}
patterncollection = Regexp.union(pattern_list)
puts patterncollection
puts test_string.scan(patterncollection)

Results are empty and if I print the pattern collection I see that "\b" doesn't get escaped correctly.
I cannot insert the "\b" directly in the array as that list gets dynamically retrieved.
I have tried more than one option but still no luck.
Different approaches to the problem are welcome.

Answer Source

The easiest solution would be to move word boundary matchers outside of the union:


▶ "lonewolf is lonely".scan /\b(#{Regexp.union(%w|lonely wolf jungle|)})\b/
#⇒ [
#    [0] [
#        [0] "lonely"
#    ]
#  ]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download