简体   繁体   中英

Regex match following substring in string python

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)

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')]

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM