Related articles 

Regular Expressions m_j_mather@yahoo.com.au (20041009) 
Re: Regular Expressions newsserver_mails@bodden.de (Eric Bodden) (20041012) 
Re: Regular Expressions randyhyde@earthlink.net (Randall Hyde) (20041012) 
Re: Regular Expressions schmitz@i3s.unice.fr (Sylvain Schmitz) (20041012) 
Re: Regular Expressions Martin.Ward@durham.ac.uk (Martin Ward) (20041012) 
Re: Regular Expressions torbenm@diku.dk (20041012) 
Re: Regular Expressions dmaze@mit.edu (David Z Maze) (20041012) 
Re: Regular Expressions Martin.Ward@durham.ac.uk (Martin Ward) (20041017) 
Re: Regular Expressions choksheak@yahoo.com (ChokSheak Lau) (20041021) 
Re: regular expressions wendt@CS.ColoState.EDU (19930322) 
Regular Expressions rafae1@hp.fciencias.unam.mx (trejo ortiz alejandro augusto) (19951016) 
Re: Regular Expressions mnp@compassda.com (Mitchell Perilstein) (19951023) 
Re: Regular Expressions cgh@cs.rice.edu (19951029) 
Re: Regular Expressions odunlain@maths.tcd.ie (Colm O'Dunlaing) (19951031) 
[3 later articles] 
From:  Martin Ward <Martin.Ward@durham.ac.uk> 
Newsgroups:  comp.compilers 
Date:  17 Oct 2004 16:03:39 0400 
Organization:  Compilers Central 
References:  0410069 0410100 
Keywords:  lex 
PostedDate:  17 Oct 2004 16:03:39 EDT 
On Tuesday 12 Oct 2004 5:56 am, Torben Ęgidius Mogensen wrote:
> A regular expression can not
> handle arbitray nesting depts, so you would either need to use a
> counter in the action of the regular expression or limit yourself to a
> fixed limit on the number of nested tables and write a regular
> expression for each level of nesting.
If you are using perl, there is another option for solving the nested
parentheses problem, which is to write a recursive regular expression:
my $np;
$np = qr{
\(
(?: # Noncapture group
(?> [^( )]+ ) # Noncapture group w/o backtracking

(??{ $np }) # Group with matching parens
)*
\)
}x;
This generates a rexexp $np which matches:
'(' followed by zero or more iterations of:
either a string with no parentheses
or something which matches $np,
followed by ')'.
In effect you have a lazy description of an infinite regexp: so no counter
is needed, nor is there a limit on the depth of parentheses.

Martin
Martin.Ward@durham.ac.uk http://www.cse.dmu.ac.uk/~mward/ Erdos number: 4
G.K.Chesterton web site: http://www.cse.dmu.ac.uk/~mward/gkc/
Return to the
comp.compilers page.
Search the
comp.compilers archives again.