简体   繁体   中英

Regex for Phone Numbers in Automate the boring stuff with python

what does this code mean? (\d{3}|\(\d{3}\)) ? I understand that (\d{3}) means that the digit is repeated 3 times, but I don't understand the rest of it. Can someone please explain

"(\d{3}|\(\d{3}\))"

What an eye- capturing regex hm?


Let's do this one by one.

\d{3} means "match any number that occurs three times."

\( or \) means "match the literal parenthesis, but this has no special meaning at all."

| means "match the previous statement OR the next statement."

( with ) means "match the regex inside me and capture/remember them."


TL;DR

Match a 3-digit number or a 3-digit number inside parentheses and capture/remeber the match.

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