Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!agate!shelby!csli!cwitty From: cwitty@csli.Stanford.EDU (Carl Witty) Newsgroups: comp.lang.perl Subject: Re: Matching parentheses Message-ID: <16600@csli.Stanford.EDU> Date: 29 Nov 90 00:53:58 GMT References: <1990Nov28.150131.28981@ugle.unit.no> Sender: cwitty@csli.Stanford.EDU Organization: CSLI, Stanford University Lines: 22 In-reply-to: harald.alvestrand@elab-runit.sintef.no's message of 28 Nov 90 15:01:31 GMT 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? There is no pattern that will do it, but here's a program that will. #!/afs/ir/@sys/local/bin/perl while () { chop; while (s/\(\)//) {} if ($_ eq "") { print "Balanced\n"; } else { print "Unbalanced\n"; } } Carl Witty cwitty@cs.stanford.edu