Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match a string against multiple patterns

Tags:

regex

ruby

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 working but I think it demonstrates at least what I am trying to do.

# example:
# prefixes.include?("Mrs. Kirsten Hess")

prefixes.include?(name) # should return true / false

prefixes = [
  /Ms\.?/i,
  /Miss/i,
  /Mrs\.?/i,
  /Mr\.?/i,
  /Master/i,
  /Rev\.?/i,
  /Reverend/i,
  /Fr\.?/i,
  /Father/i,
  /Dr\.?/i,
  /Doctor/i,
  /Atty\.?/i,
  /Attorney/i,
  /Prof\.?/i,
  /Professor/i,
  /Hon\.?/i,
  /Honorable/i,
  /Pres\.?/i,
  /President/i,
  /Gov\.?/i,
  /Governor/i,
  /Coach/i,
  /Ofc\.?/i,
  /Officer/i,
  /Msgr\.?/i,
  /Monsignor/i,
  /Sr\.?/i,
  /Sister\.?/i,
  /Br\.?/i,
  /Brother/i,
  /Supt\.?/i,
  /Superintendent/i,
  /Rep\.?/i,
  /Representative/i,
  /Sen\.?/i,
  /Senator/i,
  /Amb\.?/i,
  /Ambassador/i,
  /Treas\.?/i,
  /Treasurer/i,
  /Sec\.?/i,
  /Secretary/i,
  /Pvt\.?/i,
  /Private/i,
  /Cpl\.?/i,
  /Corporal/i,
  /Sgt\.?/i,
  /Sargent/i,
  /Adm\.?/i,
  /Administrative/i,
  /Maj\.?/i,
  /Major/i,
  /Capt\.?/i,
  /Captain/i,
  /Cmdr\.?/i,
  /Commander/i,
  /Lt\.?/i,
  /Lieutenant/i,
  /^Lt Col\.?$/i,
  /^Lieutenant Col$/i,
  /Col\.?/i,
  /Colonel/i,
  /Gen\.?/i,
  /General/i
]
like image 989
Jason Waldrip Avatar asked Mar 16 '12 03:03

Jason Waldrip


People also ask

How do you match a string to a pattern?

To match a character in the string expression against a range of characters. Put brackets ( [ ] ) in the pattern string, and inside the brackets put the lowest and highest characters in the range, separated by a hyphen ( – ). Any single character within the range makes a successful match.

What is multiline pattern match?

Pattern. MULTILINE or (? m) tells Java to accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string). Pattern.

How do I match a pattern in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .


2 Answers

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)
    #...
like image 92
mu is too short Avatar answered Sep 23 '22 06:09

mu is too short


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)
like image 14
dwerner Avatar answered Sep 21 '22 06:09

dwerner