0

I seem to be getting additional variables that I do not want stored into this array. What I expected to return after running the following code is this

[('999-999-9999'), ('999 999 9999'), ('999.999.9999')]

However what I end up with is the following

[('999-999-9999', '-', '-'), ('999 999 9999', ' ', ' '), ('999.999.9999', '.', '.')]

The following is what I have

teststr = '''
    Phone: 999-999-9999,
           999 999 9999,
           999.999.9999
'''
phoneRegex = re.compile(r'(\d{3}(-|\s|\.)\d{3}(-|\s|\.)\d{4})')

regexMatches = phoneRegex.findall(teststr)
print(regexMatches)

1 Answer 1

3

Turn the inner capturing groups to non-capturing groups.

(?:-|\s|\.)

or

[-\s.]

Example:

>>> import re
>>> teststr = '''
    Phone: 999-999-9999,
           999 999 9999,
           999.999.9999
'''
>>> re.findall(r'\b(\d{3}[-.\s]\d{3}[.\s-]\d{4})\b', teststr)
['999-999-9999', '999 999 9999', '999.999.9999']
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.