-3

I want to search all the phone numbers in a text. In this text, I found 4 different formats of phone number:

1234567890
123-456-7890
123 456 7890
(123)456-7890

I wrote a regex pattern and try it in python:

\(?\d{3}[\)\s-]?\d{3}[\s-]?\d{4}

This pattern works, I can use it to search all the phone numbers from the text. But, it also matches some wrong format such as "(1234567890", "123)456-7890" and more wrong formats. I am new to regex. Could you please help me to fix this problem? Thanks sincerely.

Harvey
  • 5,703
  • 1
  • 32
  • 41
Steven Li
  • 901
  • 1
  • 9
  • 9

1 Answers1

0

Here's something that works that you can start with.

numbers = """
1234567890
123-456-7890
123 456 7890
(123)456-7890
(1234567890
 123)456-7890
"""

import re

patterns = [
    r'\d{10}',
    r'\d{3}[ -]\d{3}[ -]\d{4}',
    r'\(\d{3}\)\d{3}-\d{4}',
]
patterns = [re.compile(p) for p in patterns]
results = []
for line in numbers.split('\n'):
    for p in patterns:
        match = p.match(line)
        if match:
            results.append(line)
Harvey
  • 5,703
  • 1
  • 32
  • 41