Path: utzoo!utgpu!watserv1!watmath!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!newcastle.ac.uk!colman!des0mpw From: des0mpw@colman.newcastle.ac.uk (M.P. Ward) Newsgroups: comp.lang.perl Subject: Re: Matching parentheses Message-ID: <1990Nov29.150720.14766@newcastle.ac.uk> Date: 29 Nov 90 15:07:20 GMT References: <1990Nov28.150131.28981@ugle.unit.no> Sender: news@newcastle.ac.uk Organization: Computing Laboratory, University of Newcastle upon Tyne, UK, NE1 7RU Lines: 55 The trick is to repetedly remove parantheses (and their contents) which don't contain parentheses. If the result has any parantheses left then the match fails. Another approach is to count +1 for (, -1 for ) and 0 for anything else. The count must never go negative and must finish at zero. Here's my perl code for checking {} balancing in LaTeX files: (I have a similar program to check \begin...\end matching) #! /usr/bin/perl # check begin/end pairs in latex file ($myname = $0) =~ s|(.*/)*||; # strip path component from name $Usage = "Usage: $myname [file]\n"; if ($#ARGV == 0) { $file = "< $ARGV[0]"; if (! -f $ARGV[0]) { die "File not found: $ARGV[0]\n"; } } elsif ($#ARGV == -1) { $file = ""; } else { die "$Usage"; } open (GOODS,"qtoa $file | egrep -n '{|}' |") || die "qtoa/egrep failed: $!"; undef $/; $* = 1; $_ = ; # slurp in begin/end pairs close GOODS; # remove \{ and \} (which don't have to match): s/\\{//g; s/\\}//g; # repeatedly collapse {...} pairs while (s/{[^{}]*}//g) { 1; } # eliminate junk lines (with no { or }): while (s/^[^{}]*\n//g) { 1; } # print the result: print; Martin. JANET: Martin.Ward@uk.ac.durham Internet (eg US): Martin.Ward@DURHAM.AC.UK or if that fails: Martin.Ward%uk.ac.durham@nfsnet-relay.ac.uk or even: Martin.Ward%DURHAM.AC.UK@CUNYVM.CUNY.EDU BITNET: IN%"Martin.Ward@DURHAM.AC.UK" UUCP:...!mcvax!ukc!durham!Martin.Ward