Re: regexp: ignoring nested brackets



I just did a little revisement on Jeffrey's code and apply it on the
problem of OP..He actually counted the numbers of the opening and
closing brackets to control the matching of regex..
#############################################
$_='\mymacro{abcd {abcdgte{asdfSA}ksd} {ASDFD}sdfsd} ;laksjd;lkfajs';

my $pattern = qr{
(?> (?{$b=0}) # count the number of braces
(?:
# Stuff not braces
[^{}]+
# An opening brace
| \{ (?{$b++})
# A closing brace
| \} (?(?{$b !=0 }) (?{$b--}) | (?!))
)*
) (?(?{$b != 0}) (?!)) #fail if no balanced brace found
}x;

print "$1\n" while /\s\{($pattern)\}/g;
##############################################
#the resulting output:
abcdgte{asdfSA}ksd
ASDFD
##############################################
Looks good so far, didnt try some more complex data anyway....
Xicheng

robic0 wrote:
> On 2 Jan 2006 17:07:17 -0800, "Xicheng" <xicheng@xxxxxxxxx> wrote:
>
> >try: perldoc -q nest
> >
> >you may also refer to Jeffery's "Mastering Regular Expressions Second
> >Edition", Chapter-7 section 7.8.5 "Matching Nested Constructs with
> >Embedded Code", which proposed a way to do it in a sigle regex.
> >
> >Xicheng
>
> I didn't read the book, but just to qualify things a bit.
> This phrase:
> "Matching Nested Constructs with Embedded Code"
>
> has "nested contstucts" in it. Constructs means a
> beginning form of the phrase. It also means a multiple
> character phrase. Thats a problem in regex. The engine is
> in dire need of an overhaul, invoking the ire of those that
> critiscise Perl in any way. The dilema is that a "phrase" cannot
> be treated as a entity in ALL regular expression constructs.
>
> Its a serious flaw and requires a redisign of the engine.

.



Relevant Pages

  • Re: regexp: ignoring nested brackets
    ... >closing brackets to control the matching of regex.. ... the true "character" nature of regex. ... a class item can include a phrase... ... a phrase level. ...
    (comp.lang.perl.misc)
  • Re: 40 years of the Ford Transit...
    ... the Grimy troll said that any engine that combusted ... Hint: it includes the phrase "system boundary". ...
    (uk.rec.cars.classic)
  • Re: Lamps not Bulbs
    ... The phrase "siege engine" dates about to the 13th century. ... Two root Latin words- engenium and sedere. ... Although the phrase & song "Hello John, got a new motor" doesn't refer to a device with a lead hanging out of it. ...
    (rec.arts.theatre.stagecraft)
  • Re: replace variable with same variable
    ... David wrote: ... variable into the regex replace phrase or can it be done within the ... -- Larry Wall ...
    (comp.lang.perl.misc)
  • Re: Match and cut regex?
    ... [This followup was posted to comp.lang.perl.misc] ... > If I have a 'cut' phrase: ... > What regex do I use to match the ABBCCCD and then chop off everything ... use English; ...
    (comp.lang.perl.misc)