Get all strings matching given RegExp

Paul McGuire ptmcg at austin.rr.com
Thu Apr 3 13:33:28 EDT 2008


On Apr 3, 4:50 am, Alex9968 <noname9... at gmail.com> wrote:
> Can I get sequence of all strings that can match a given regular
> expression? For example, for expression '(a|b)|(x|y)' it would be ['ax',
> 'ay', 'bx', 'by']
>

Actually, this regex will only match 'a', 'b', 'x', or 'y' (assuming
you meant to bracket it with leading '^' and trailing '$').  To get
the set you listed, you would need to invert '(a|b)(x|y)'.

The thread that Gabriel Genellina referenced includes a link to a
pyparsing regex inverter (http://pyparsing-public.wikispaces.com/space/
showimage/invRegex.py), which I used to test your regex - I've added
both regexes in the test cases of that script.  Some of the other test
cases include regexes for all US time zones, and all chemical symbols.

-- Paul



More information about the Python-list mailing list