Simon Brunning wrote:
On 4/21/05, rbt <[EMAIL PROTECTED]> wrote:

string.between(data,[,])


def between(data, start, end):
return re.findall(re.escape(start) + r'([^]]*)'+ re.escape(end), data)

That's cool! But it doesn't quite work if the end tag is not ']':

>>> import re
>>> def between(data, start, end):
...     return re.findall(re.escape(start) + r'([^]]*)'+ re.escape(end), data)
...
>>> foo = '''<stuff>   [lsass.exe]
... [System]  <more> stuff
... xxxxx<qqq> [firefox.exe] ......
... '''
>>> print between(foo, '[', ']')
['lsass.exe', 'System', 'firefox.exe']
>>> print between(foo, '<', '>')
['stuff', 'more> stuff\nxxxxx<qqq']


Here's a revised version that will work with other tags:

>>> def between2(data, start, end):
... pattern = re.escape(start) + ' # start tag \n' +\
... r'([^' + re.escape(end) + r']*)' + " # anything except end tag \n" +\
... re.escape(end) + ' # end tag \n'
... return re.findall(pattern, data, re.VERBOSE)
...
>>> print between2(foo, '[', ']')
['lsass.exe', 'System', 'firefox.exe']
>>> print between2(foo, '<', '>')
['stuff', 'more', 'qqq']


Regards,
Jim Sizelove
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to