How can I match a string against multiple patterns using regular expression in ruby.
I am trying to see if a string is included in an array of prefixes, This is not
If you can use a single string, it might be faster to write a regex containing the possible values.
e.g.
/(Mr\.|Mrs\.| ... )/.match(name)
Use Regexp.union to combine them:
union(pats_ary) → new_regexp
Return a
Regexp
object that is the union of the given patterns, i.e., will match any of its parts.
So this will do:
re = Regexp.union(prefixes)
then you use re
as your regex:
if name.match(re)
#...