regular expression help
Drew Fisher
drew at level3.net
Wed Feb 27 18:38:32 EST 2002
Hello,
I'm having some issues doing a small but irritating regular expression
match.
Here's an example of text that I want to search through:
str = """
Location:Home
555-1212
555-3434
555-5656
Location:Work
555-9999
"""
Just for reference, there is newline characters at the end of each line
and the phone numbers have a single tab character (\t) in front of them.
I'm trying to get the location of the record and the associated phone
number(s) that go along with it.
I've tried things like:
import re
test1 = re.compile ('Location:(\w+)\s+(\d+\-\d+)', re.DOTALL)
print test1.findall (str)
[('Home', '555-1212'), ('Work', '555-9999')]
But this only gets the location and the first phone number in the
record. How do I get all of the phone numbers for each location without
writing a second regular expression?
Thanks for your help,
Drew
More information about the Python-list
mailing list