1

Possible Duplicate:
Python strings split with multiple separators

Is there a way in Python to split a string on two different keys?

Say I have a string like this:

mystr = "wketjwlektjwltjkw<br/>wwwweltjwetlkwjww" + \
         "wwetlwjtwlet<strong>wwwwketjwlektjwlk</strong"

I'd like to split on EITHER <br/>wwww or <strong>wwww.

How should I do this?

Thanks!

Community
  • 1
  • 1
AP257
  • 89,519
  • 86
  • 202
  • 261
  • 1
    Possible duplicate of [Python strings split with multiple separators](http://stackoverflow.com/questions/1059559/python-strings-split-with-multiple-separators). Short answer: use a regular expression. – Frédéric Hamidi Dec 20 '10 at 12:54
  • Oh yes, it is a duplicate, sorry about that! – AP257 Dec 20 '10 at 12:56
  • Do you want that if both the characters exist the string gets divided into 3 lists ??? mystr.split('
    ' or 'wwww>') works if you want that whichever exists first gets split into 2 lists....
    –  Dec 20 '10 at 12:57
  • 2
    No! That will evaluate `'
    ' or 'wwww>'` first, resulting in `'
    '`.
    – Lucas Moeskops Dec 20 '10 at 12:59

1 Answers1

8
import re
re.split(r'<(br\/|strong)>wwww', mystr)[::2]
Lucas Moeskops
  • 5,445
  • 3
  • 28
  • 42