How to print all expressions that match a regular expression

S

Steve Holden

Steven said:
Oh, everybody expects parrots! That's not unexpected -- as a clue, I
wrote that "the message is predictable for being totally unexpected".

The input was "Nobody expects the Spanish Inquisition!", which is another
Monty Python catchphrase.
Bugger - Got everything except the trailing exclamation mark ...

regards
Steve
 
P

Paul McGuire

Hi,

I am a fresh man with python. I know there is regular expressions in
Python. What I need is that given a particular regular expression,
output all the matches. For example, given “[1|2|3]{2}” as the regular
expression, the program should output all 9 matches, i.e., "11 12 13
21 22 23 31 32 33".

Is there any well-written routine in Python or third-party program to
do this? If there isn't, could somebody make some suggestions on how
to write it myself?

Thanks.

Zhuo

Please check out this example on the pyparsing wiki, invRegex.py:
http://pyparsing.wikispaces.com/file/view/invRegex.py. This code
implements a generator that returns successive matching strings for
the given regex. Running it, I see that you actually have a typo in
your example.
print list(invert("[1|2|3]{2}"))
['11', '1|', '12', '13', '|1', '||', '|2', '|3', '21', '2|', '22',
'23', '31', '3|', '32', '33']

I think you meant either "[123]{2}" or "(1|2|3){2}".
['11', '12', '13', '21', '22', '23', '31', '32', '33']
['11', '12', '13', '21', '22', '23', '31', '32', '33']

Of course, as other posters have pointed out, this inverter does not
accept regexen with unbounded multiple characters '+' or '*', but '?'
and "{min,max}" notation will work. Even '.' is supported, although
this can generate a large number of return values.

Of course, you'll also have to install pyparsing to get this to work.

-- Paul
 
H

hzhuo1

Please check out this example on the pyparsing wiki, invRegex.py:http://pyparsing.wikispaces.com/file/view/invRegex.py.  This code
implements a generator that returns successive matching strings for
the given regex.  Running it, I see that you actually have a typo in
your example.
print list(invert("[1|2|3]{2}"))

['11', '1|', '12', '13', '|1', '||', '|2', '|3', '21', '2|', '22',
'23', '31', '3|', '32', '33']

I think you meant either "[123]{2}" or "(1|2|3){2}".
print list(invert("[123]{2}"))

['11', '12', '13', '21', '22', '23', '31', '32', '33']

['11', '12', '13', '21', '22', '23', '31', '32', '33']

Of course, as other posters have pointed out, this inverter does not
accept regexen with unbounded multiple characters '+' or '*', but '?'
and "{min,max}" notation will work.  Even '.' is supported, although
this can generate a large number of return values.

Of course, you'll also have to install pyparsing to get this to work.

-- Paul


Hi Paul,

Thanks very much. This is exactly what I need now. I will check this
function.

Zhuo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top