Path: utzoo!utgpu!watserv1!watmath!att!att!pacbell.com!ucsd!usc!sdd.hp.com!wuarchive!udel!haven!decuac!bacchus.pa.dec.com!bacchus!mwm From: mwm@raven.relay.pa.dec.com (Mike (My Watch Has Windows) Meyer) Newsgroups: comp.lang.perl Subject: Re: Matching parentheses Message-ID: Date: 28 Nov 90 22:22:14 GMT References: <1990Nov28.150131.28981@ugle.unit.no> <582@sunny.ucdavis.edu> Sender: news@wrl.dec.com (News) Organization: Missionaria Phonibalonica Lines: 25 In-Reply-To: poage@sunny.ucdavis.edu's message of 28 Nov 90 22:12:10 GMT In article <582@sunny.ucdavis.edu> poage@sunny.ucdavis.edu (Tom Poage) writes: 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? Mr. Poage's solution doesn't work - it thinks that )( is a pair of matching parens. I don't think raw count was what was intended. >(or quotes, for that matter...) This also depends on what you mean by "matching quotes". If all you want is for every quote character to have a match later, this does it: /^[^"]*("[^"]*"[^"]*)*$/ You can incorporate escaped quotes into that pattern (left as an exercise). If you want to correctly deal with two kinds of quotes, I think this does it, but haven't done any testing on it: /^[^"']*((("[^"]*")|('[^']*'))[^"']*)*$/