Home arrow static arrow Java Programming [Archive] - Regular Expression Help
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Regular Expression Help
This topic has 4 replies on 1 page.

Posts:31
Registered: 5/30/03
Regular Expression Help  
Jun 30, 2004 11:06 AM



 
I need the ability to match in a regular expression that keeps track of how many times a pair of parentheses exists. For example, right now, I have it matching the largest possible match of an openining and closing parenthesi; however, those openining a closing parentheses may not correspond to each other.

Visually,
( a(b )c )     ((d ) ( e))


would incorrectly match as
( a(b )c )     (d ) ( e)
, instead of correctly matching as
( a(b )c )
in the first match, and
((d ) ( e))
in the second large group match (in addition, to the inner matches). Any ideas / suggestions on which quantifiers (or other regex expressions) to use?
 

Posts:24,036
Registered: 2/3/03
Re: Regular Expression Help  
Jun 30, 2004 11:25 AM (reply 1 of 4)



 
I need the ability to match in a regular expression
that keeps track of how many times a pair of
parentheses exists.

Not a good use of regular expressions, IMO. Better to use a stack or some other such method....
 

Posts:2,391
Registered: 9/26/00
Re: Regular Expression Help  
Jun 30, 2004 11:28 AM (reply 2 of 4)



 
Sorry, but you can't do that with regular expressions. Specifically, you can't use a regex to match paired delimiters that can be nested to an arbitrary depth. You either have to take a completely manual approach, or use a parser generator like [url=http://antlr.org/]ANTLR[/url].
 

Posts:49
Registered: 6/23/04
Re: Regular Expression Help  
Jun 30, 2004 1:00 PM (reply 3 of 4)



 
I am not sure I completely understand the question, but woudln't it be simpler to count the number of open and closed parenthesis? Whenever you hit '(' you increase the counter, and whenever you hit ')' you decrease it.

Whenever the counter transitions from 0 to 1 you mark the beginning of the group, and whenever it transitions from 1 to 0 you spit out a group.
                 ( a ( b ) ) ( ( d ) ( e ) )counter =  0 0 0 1 1 2 2 1 0 1 2 2 1 2 2 1 0 0 0 0 0 1                                           ^ end                             ^ start                           ^ end                 ^ start
 

Posts:31
Registered: 5/30/03
Thanks  
Jul 1, 2004 8:45 AM (reply 4 of 4)



 
I am not sure I completely understand the question,
but woudln't it be simpler to count the number of open
and closed parenthesis? Whenever you hit '(' you
increase the counter, and whenever you hit ')' you
decrease it.

Whenever the counter transitions from 0 to 1 you mark
the beginning of the group, and whenever it
transitions from 1 to 0 you spit out a group.
( a ( b ) ) ( ( d ) ( e ) )counter =  0 0 0 1 1 2 2 1 0 1 2 2 1 2 2 1 0 0 0 0 0 1^ end^ start^ end^ start

Yes, you're right, it would be easier to just count the number of parentheses. However, the existing code is all RegExp based, so using patterns would just be a cleaner fit (even though it probably would have been better to not use them in the first place). Thanks for the help, in any event.
 
This topic has 4 replies on 1 page.