0

I had a script that scanned in piece of text and returned me a group which I would save to an array. The code looks like this:

pattern = re.compile(r'<span id="first_name">(.+?)</span>')
matches = pattern.findall(str(my_text_file))

This works awesome and I could scan first names in my text file and write them into an array doing this:

for firstname in matches:
    if firstname not in list_of_names:
        list_of_names.append(firstname)

But now I need to expand my pattern to retrieve two groups instead of one, and I have no idea how I am supposed to get to the second group.

When I have something like:

pattern = re.compile(r'<span id="first_name">(.+?)</span><span id="last_name">(.+?)</span>')
matches = pattern.findall(str(my_text_file))

How am I supposed to put those second group (last names) in a different array?

2
  • What have you tried? Have you tried looking at what pattern.findall is returning? Have you read the documentation to see what it should be returning? Commented Nov 27, 2011 at 15:29
  • python documentation say : "return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match." so i guess I will be getting a list of tuples... but i dont understand this because I do not natively speak english.... Commented Nov 27, 2011 at 15:32

1 Answer 1

2
for match in matches:
    first_names.append(match[0])
    last_names.append(match[1])
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.