brackets content regular expression

Matimus mccredie at gmail.com
Fri Oct 31 13:53:22 EDT 2008


On Oct 31, 10:25 am, netimen <neti... at gmail.com> wrote:
> I have a text containing brackets (or what is the correct term for
> '>'?). I'd like to match text in the uppermost level of brackets.
>
> So, I have sth like: 'aaaa 123 < 1 aaa < t bbb < a <tt  > ff > > 2 >
> bbbbb'. How to match text between the uppermost brackets ( 1 aaa < t
> bbb < a <tt  > ff > > 2 )?
>
> P.S. sorry for my english.

I think most people call them "angle brackets". Anyway it should be
easy to just match the outer most brackets:

>>> import re
>>> text = "aaaa 123 < 1 aaa < t bbb < a <tt  > ff > > 2 >"
>>> r = re.compile("<(.+)>")
>>> m = r.search(text)
>>> m.group(1)
' 1 aaa < t bbb < a <tt  > ff > > 2 '

In this case the regular expression is automatically greedy, matching
the largest area possible. Note however that it won't work if you have
something like this: "<first> <second>".

Matt



More information about the Python-list mailing list