1

Please help me fix this regular expression check.

x=re.match('^(\d{3})\s\d{3}-\d{4}$','(800) 325-3535')

It is supposed to return match object but what I get is None value. Am I doing anything wrong over here. Please help.

2
  • And it is supposed to match what? What is the input? Commented May 28, 2014 at 6:27
  • @Gwenc37 Phone numbers in the format as (800) 325-3535 Commented May 28, 2014 at 7:02

1 Answer 1

5

You should escape the () by backslash:

^\(\d{3}\)\s\d{3}-\d{4}$

Like this:

x = re.match('^\(\d{3}\)\s\d{3}-\d{4}$','(800) 325-3535')

() means capturing groups in regex and whatever symbol has a special meaning in regex should be escaped to be used in its literal form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.