regex for balanced parentheses?

Tim Arnold tim.arnold at
Thu Jun 12 18:38:41 CEST 2008

"Paul McGuire" <ptmcg at> wrote in message 
news:206a91d7-8251-49f6-b500-e47af4887a10 at
> Parsing TeX is definitely not for the faint-of-heart!  You might try
> something like QuotedString('$', escQuote='$$') in pyparsing.  (I've
> not poked at TeX or its ilk since the mid-80's so my TeXpertise is
> long rusted away.)
> I know of two projects that have taken on the problem using pyparsing
> - one is the mathtext module in John Hunter's matplotlib, and Tim
> Arnold posted some questions on the subject a while back - try
> googling for "pyparsing tex" for further leads.
> -- Paul

Definitely agree that TeX can get pretty complicated. My method (writing a 
converter from one TeX tag system to another)  was to pre-parse using string 
match/replace for really simple stuff, regular expressions for the more 
complex and pyparsing for the really tough stuff.

One thing that was surprisingly hard for me to figure out was filtering out 
comments. I finally just looped through the file line by line, looking for a 
'%' that wasn't in a verbatim environment and wasn't escaped, etc.
Funny how sometimes the simplest thing can be difficult to handle.

Definitely pyparsing made the job possible; I can't imagine that job without 

--Tim Arnold

More information about the Python-list mailing list