like a "for loop" for a string

Vlastimil Brom vlastimil.brom at gmail.com
Sun Aug 17 17:09:13 EDT 2008


Ok, after sending my post, I see, that the code got a bit screewed up, but
morover, there should probably be a word boundary in the closing part of the
regexp. A next attempt + added stripping the "yes" delimiter and the
whitespace...:

>>> import re
>>> input_text = """yes text1 yes text2 yes text3 no text4 yes text5+more
Text yes text6 no text7 yes text8 no text9"""
>>> yes_parts = re.findall(r"(?s)\byes\b.*?(?=\byes\b|\bno\b|$)",
input_text)
>>> yes_parts_infos = [item[3:].strip() for item in yes_parts]
>>> yes_parts
['yes text1 ', 'yes text2 ', 'yes text3 ', 'yes text5+more Text ', 'yes
text6 ', 'yes text8 ']
>>> yes_parts_infos
['text1', 'text2', 'text3', 'text5+more Text', 'text6', 'text8']
>>>

HTH,

  Vlasta
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080817/3dbd910d/attachment-0001.html>


More information about the Python-list mailing list