0

I've come up with a regex expression that works well enough for my purposes for finding phone numbers.

I would like to take it a step further and use it in large text blocks to identify matching strings that follow the words 'cell' or 'mobile' by at most 10 characters. I would like it to return the number in Cell Phone: (954) 555-4444 as well as Mobile 555-777-9999 but not Fax: (555) 444-6666

something like (in pseudocode)

regex = re.compile(r'(\+?[2-9]\d{2}\)?[ -]?\d{3}[ -]?\d{4})')
bigstring = # Some giant string added together from many globbed files
matches = regex.search(bigstring)
for match in matches:
    if match follows 'cell' or match follows 'mobile':
        print match.group(0)
1
  • are you just trying to ignore the fax numbers? Commented Mar 25, 2015 at 18:48

2 Answers 2

1

You can do:

txt='''\
Call me on my mobile anytime: 555-666-1212 
The office is best at 555-222-3333 
Dont ever call me at 555-666-2345 '''

import re

print re.findall(r'(?:(mobile|office).{0,15}(\+?[2-9]\d{2}\)?[ -]?\d{3}[ -]?\d{4}))', txt)

Prints:

[('mobile', '555-666-1212'), ('office', '555-222-3333')]
Sign up to request clarification or add additional context in comments.

2 Comments

this solution does not even work for the provided example Cell Phone: (954) 555-4444 ... not because of the word Cell but because it does not match the first parenthesis
Yes. You can put whatever inside the first capture group. Play with it here
0

You can do that with your regular expression. In the re documentation, you will find that the pattern r'(?<=abc)def' matches 'def' only if it is preceded by 'abc'.

Similarly r'Hello (?=World)' matches 'Hello ' if followed by 'World'

2 Comments

I added that it needs to be able to match even if there are intermediate words or symbols - will this work for that?
it can work but you will have to adapt your regex a bit

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.