Wednesday, August 3, 2011

Quick regex match example in python

This is an example of matching the "blahblah" that is between "this" and the first "end" (i.e. it's not greedy).

>>> s = 'try to match thisblahblahend and not that end or else...'
>>> r = re.compile('this(?P.+?)end')
>>> m = r.search(s)
>>> m.group('my_match')
'blahblah'

No comments:

Post a Comment