trying to find repeated substrings with regular expression

johnzenger at gmail.com johnzenger at gmail.com
Mon Mar 13 23:33:11 CET 2006


Robert Dodier wrote:

> I've decided it's easier for me just to search for FOO, and then
> break up the string based on the locations of FOO.
>
> But I'd like to better understand regular expressions.

Those who cannot learn regular expressions are doomed to repeat string
searches.  Which is not such a bad thing.

txt = "blah FOO blah1a blah1b FOO blah2 FOO blah3a blah3b blah3b"

def fa(s, pat):
    retlist = []
    try:
        while True:
            i = s.rindex(pat)
            retlist.insert(0,s[i:])
            s = s[:i]
    except:
        return retlist

print fa(txt, "FOO")




More information about the Python-list mailing list