简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM