trying to find repeated substrings with regular expression

Kent Johnson kent at kentsjohnson.com
Mon Mar 13 19:53:22 CET 2006


Robert Dodier wrote:
> Hello all,
> 
> I'm trying to find substrings that look like 'FOO blah blah blah'
> in a string. For example give 'blah FOO blah1a blah1b FOO blah2
> FOO blah3a blah3b blah3b' I want to get three substrings,
> 'FOO blah1a blah1b', 'FOO blah2', and 'FOO blah3a blah3b blah3b'.
> 
> I've tried numerous variations on '.*(FOO((?!FOO).)*)+.*'
> and everything I've tried either matches too much or too little.

FOO(.*?)(?=FOO|$)


> 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.

Use re.split() for this.

Kent



More information about the Python-list mailing list