[Tutor] Generating all possible hits of a regex pattern

Kent Johnson kent37 at tds.net
Wed Jan 31 14:35:51 CET 2007


Barton David wrote:
> I don't suppose there's a nifty trick (exploiting the internal workings 
> of the re module perhaps) to generate all possible hits of a given regex 
> pattern?
>  
> Something like:
>  
>> >> import re
>> >> mypattern=re.compile( "[AB]C{1,2}D?" )
>> >> print list( all_pattern_generator( mypattern ) )
> ["AC","BC","ACC","BCC","ACD","BCD","ACCD","BCCD"]
>  
> Or would I have to do this the hard way, parsing the expression myself etc?

See this discussion:
http://tinyurl.com/22xsxg

Kent



More information about the Tutor mailing list