recording data between [ and ]
Jim Sizelove
sizelji at insightbb.com
Thu Apr 21 13:33:56 EDT 2005
Simon Brunning wrote:
> On 4/21/05, rbt <rbt at athop1.ath.vt.edu> 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
More information about the Python-list
mailing list