Ben
Ben

Reputation: 1342

ruby regex not matching a string if "+" character in source

i have a string that contains an international phone number e.g. +44 9383 33333 but bizzarely when I attempt to regex match (note the regex is correctly 'escaped') the regex match fails

e.g.

 "001144 9383 33333".match(/(001144|004|0|\\+44)/) # works

 "+44 9383 33333".match(/(001144|004|0|\\+44)/) # DOES NOT WORK

i've tried escaping the input string e.g. +, \+ etc. etc but to no avail.

i must be doing something really stupid here!?

Upvotes: 1

Views: 69

Answers (1)

neuronaut
neuronaut

Reputation: 2699

You've got a double backslash, which is telling the regex parser to look for a literal backslash. Since it's also followed by a + the regex parser is then looking for 1 or more backslashes. Try just \+ (so the whole thing should be: /(001144|004|0|\+44)/

Upvotes: 7

Related Questions