Regexp problem for gurus
Pekka Niiranen
krissepu at vip.fi
Sun Jan 6 15:04:44 EST 2002
How can I make python to return list of matches in the following case:
The searched texts are between ?- and ! -signs
Different start (?) and end (!) -signs are used in order to able
to detect nested matches.
case 1: Line = "aaa?HHH!bbb?JJJJ!ccc?KKKK!dddd
Returned list should be:
?HHH!, ?JJJJ!, ?KKKK!
For this one I allready know the answer:
pattern = re.compile(r'?[^?!]+!')
list = re.findall(pattern, Line)
case 2: Line = "aaa?HHHbbb?JJJJ!ccc!?KKKK!dddd
Returned list should be (the order of matches does
not matter):
?HHHbbb?JJJJ!ccc!, ?JJJJ!, ?KKKK!
In this case ?JJJJ! is nested inside
?HHHbbb?JJJJ!ccc!
The amount of nested patterns is limited by the
python ?
Is it possible to compile a single regular expression that covers both
the cases ?
Howabout if the limiting signs consist more than one characters
(not ? and !, but <?> and <!>)? Then negation group [^] would not work ?
-pekka-
More information about the Python-list
mailing list