Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!letni!mic!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: Matching parentheses Message-ID: <109450@convex.convex.com> Date: 29 Nov 90 00:18:12 GMT References: <1990Nov28.150131.28981@ugle.unit.no> Sender: news@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 16 In article <1990Nov28.150131.28981@ugle.unit.no> harald.alvestrand@elab-runit.sintef.no writes: >I have thought a long time about this, but I am sure there must be some >magic way to do it: >How can I write a pattern that matches (), ((())), ((())()), but NOT ((())? >That is, how do I get a balanced matching of parentheses? >(or quotes, for that matter...) No, there's no "magic" way of doing it. The problem is that regular expressions are really nice, but they just can't handle recursive structures -- at least, not by themselves. To deal with these in Perl, you could interatively process /\(([^\)]*)\)/ to deal with things from the inside-out. Simple approaches like that one won't work well with escaped characters; you'll need to change them into something else (like \201) before the search, or else you'll match where you don't want to. --tom