like a "for loop" for a string

Vlastimil Brom vlastimil.brom at gmail.com
Sun Aug 17 16:42:16 EDT 2008


2008/8/17 Alexnb <alexnbryan at gmail.com>

>
> Uhm, "string" and "non-string" are just that, words within the string. Here
> shall I dumb it down for you?
>
>
> string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
> text6  no text7 yes text8"
>
> It doesn't matter what is in the string, I want to be able to know exactly
> how many "yes"'s there are.
> I also want to know what is after each, regardless of length. So, I want to
> be able to get "text1", but not "text4" because it is after "no" and I want
> all of "text5+more Text" because it is after "yes". It is like the yeses
> are
> bullet points and I want all the info after them. However, all in one
> string.
>
>
> As already has been suggested, you could use regular expressions for that
(if there isn't some more complex parsing necessary).
Maybe something like this could work:
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.*?(?=yes|no|$)", input_text)
yes_parts
['yes text1 ', 'yes text2 ', 'yes text3 ', 'yes text5+more Text ', 'yes
text6 ', 'yes text8 ']

 The pattern searches for the string "yes" followed by the shortest possible
arbitrary text until next "yes", "no" or the end of input string is reached.
"\b" should prevent matching "yes" as a part of other words, "(?s)" lets the
"." match newlines, in case there would be multiline input.
Possibly the trailing spaces could be handled specifically if necessary.
Vlasta
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080817/70747f3c/attachment-0001.html>


More information about the Python-list mailing list