python-regexHow to match a phone number in Python regex?
The following regex can be used to match a phone number in Python:
\d{3}-\d{3}-\d{4}
This regex will match a phone number in the format of 123-456-7890
. The \d
part of the regex stands for a digit (0-9), and the {3}
and {4}
indicate that the preceding digit should appear 3 and 4 times respectively.
Code explanation
\d
: Matches any digit (0-9){3}
: Indicates that the preceding digit should appear 3 times{4}
: Indicates that the preceding digit should appear 4 times
Helpful links
More of Python Regex
- How to match a year with Python Regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match a URL path using Python regex?
- How to use word boundaries in Python Regex?
- How to replace all using Python regex?
- How to replace using Python regex?
- How to match a hex number with regex in Python?
- How to get all matches from a regex in Python?
- How to perform a zero length match with Python Regex?
- How to get a group from a regex in Python?
See more codes...