brackets content regular expression
netimen
netimen at gmail.com
Fri Oct 31 19:57:10 CET 2008
Thank's but if i have several top-level groups and want them match one
by one:
text = "a < b < с > d > here starts a new group: < e < f > g >"
I want to match first " b < с > d " and then " e < f > g " but not "
b < с > d > here starts a new group: < e < f > g "
On 31 окт, 20:53, Matimus <mccre... at gmail.com> wrote:
> 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