Is there a re guru out there?

Pekka Niiranen krissepu at vip.fi
Mon Jul 8 08:13:51 EDT 2002


Try modifing this example:

----code starts ----

>>> pattern = re.compile("(\?[^?!]+(\?[^?!]+\!)*[^?!]+\!)")
>>> Line = "?AA?BB!CC!?DD!ee?EE!ff?FF?GG!HH!"
>>> print re.findall(pattern, Line)
>>> [('?AA?BB!CC!', '?BB!'), ('?DD!', ''), ('?EE!', ''), ('?FF?GG!HH!',
'?GG!')]


----code ends ----

The code above searches strings that are between ?- and ! -signs.
Only one nested level is allowed. Anyways, just add more levels
inside group (\?[^?!]+\!)*  and possibly change some + -signs to *?:

pattern = re.compile("(\?[^?!]*?(\?[^?!]+\!)*[^?!]*?\!)")


-pekka-

Krzysiek Czarnowski wrote:

> Hello!
> And thanks for the prompt answer.
>
> Daniel Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
> <cut>
> > Ah, a LaTeX to TeX converter.  Cool!
>
> Well, rather a simple tool to reduce the amount of manual work required...
>
> <cut>
> > If you can guarantee that there isn't any nested braces within the "frac"
> groups
> <cut>
>
> I would like to accept nested braces (at least to some extent)...
> Later I will look into your examples closer.
>
> <cut>
> > However, if there are groups within the frac groups, like:
> >
> >    \frac{e^{42}}{\pi}
> >
> > then that will cause problems.  This kind of pattern is something that
> > regular expressions alone won't be able to cope with, because the
> > grammar is recursively defined.
>
> That's what I was afraid of...
> Probably I will have to do with a solution accepting only finite level of
> nesting...
>
> Thanks anyway,
> Krzysztof




More information about the Python-list mailing list