0

This is my string:

<span class="word">blue</span><span class="word">red</span><span class="word">yellow</span><span class="word">orange</span>

Usually I would use this just to get one result into a variable:

result = re.search('(<span class="word">)(.*)(</span>)', string)
color = result.group(2)

But now I want to get every result from my string and store each into a list. How would I go about doing this?

1
  • You should use BeautifulSoup for such purpose. Commented May 11, 2012 at 18:00

2 Answers 2

1

There is re.findall. For larger strings I recommend re.finditer.

Sign up to request clarification or add additional context in comments.

Comments

0

use findall instead of search

findall() Find all substrings where the RE matches, and returns them as a list.

finditer() Find all substrings where the RE matches, and returns them as an iterator.

http://docs.python.org/howto/regex.html

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.