user3352330
user3352330

Reputation:

Regular Expression using Python

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.

Upvotes: 1

Views: 61

Answers (1)

Amit Joki
Amit Joki

Reputation: 59232

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.

Upvotes: 5

Related Questions