Python Regular Expression to extract phone number

python-regular-expression-extract-phone-number-feature-image

In this tutorial, we’re gonna look at way to use Python Regular Expression to extract phone number from a text.

Related Post: Python Regular Expression

Python Regular Expression to extract phone number

Import the regex module

All Python regex functions in re module. Remember to import it at the beginning of Python code or any time IDLE is restarted.

>>> import re
Create Regex object

We create a Regex object by passing a string value representing regular expression to re.compile().

To match the phone number pattern:

>>> regex = re.compile(r'''(
...    [(]?(\d{3})?[)]? # area code
...    (\s|-|\.)? # separator
...    (\d{3}) # first 3 digits
...    (\s|-|\.) # separator
...    (\d{4}) # last 4 digits
...    (\s|,) # separator
...    (\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
... )''', re.VERBOSE | re.IGNORECASE)
Get Match object

Regex object has search() method that searches the string that matches to the regex. It returns:
None if the regex pattern is not found
– a Match object if the pattern is found

# text = 'Please contact us at (123) 555-6789, ext 168.'
mo = regex.search(text)
Get matched text

We call Match object’s group() method to get the actual matched text from the searched string.

>>> mo.group()
# '(123) 555-6789, ext 168'
>>> mo.group(2)
'123'
>>> mo.group(4)
'555'
>>> mo.group(6)
'6789'
>>> mo.group(10)
'168'

In this example, we use parentheses to group the pattern into several groups, so we can call groups() method that returns a tuple of multiple values.

>>> mo.groups()
# ('(123) 555-6789, ext 168', '123', ' ', '555', '-', '6789', ',', ' ext 168', 'ext', '168')
Extract phone number

Now we want the phone number to be in a standard format. So we store it in phoneNumber variable that are built from groups 2, 4, 6, and 10 of the matched text above (area code, first 3 digits, last 4 digits, extension).

>>> phoneNumber = '-'.join([mo.group(2), mo.group(4), mo.group(6)])
>>> if mo.group(10) != '':
...     phoneNumber += ' x.' + mo.group(10)
...
>>> phoneNumber
'123-555-6789 x.168'

Full code

>>> import re

>>> regex = re.compile(r'''(
...    [(]?(\d{3})?[)]? # area code
...    (\s|-|\.)? # separator
...    (\d{3}) # first 3 digits
...    (\s|-|\.) # separator
...    (\d{4}) # last 4 digits
...    (\s|,) # separator
...    (\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
... )''', re.VERBOSE | re.IGNORECASE)

>>> text = 'Please contact us at (123) 555-6789, ext 168.'
>>> mo = regex.search(text)

>>> phoneNumber = '-'.join([mo.group(2), mo.group(4), mo.group(6)])
>>> if mo.group(10) != '':
...     phoneNumber += ' x.' + mo.group(10)
...
>>> phoneNumber
'123-555-6789 x.168'

4 thoughts on “Python Regular Expression to extract phone number”

  1. Thanks for sharing your ideas listed here. The other matter is that each time a problem comes up with a computer motherboard, people today should not have some risk connected with repairing the item themselves for if it is not done properly it can lead to permanent damage to an entire laptop. It is usually safe just to approach your dealer of any laptop for any repair of motherboard. They have technicians who’ve an knowledge in dealing with mobile computer motherboard troubles and can make the right diagnosis and perform repairs.

  2. I抦 impressed, I must say. Really not often do I encounter a blog that抯 each educative and entertaining, and let me let you know, you’ve hit the nail on the head. Your concept is outstanding; the problem is one thing that not sufficient people are speaking intelligently about. I’m very comfortable that I stumbled across this in my seek for something regarding this.

  3. Thanks, I have recently been searching for information approximately this topic for a while and yours is the greatest I have discovered till now. However, what concerning the conclusion? Are you certain in regards to the supply?

Leave a Reply

Your email address will not be published. Required fields are marked *