Re: regexp: ignoring nested brackets
- From: "Xicheng" <xicheng@xxxxxxxxx>
- Date: 2 Jan 2006 17:40:32 -0800
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.
.
- Follow-Ups:
- Re: regexp: ignoring nested brackets
- From: robic0
- Re: regexp: ignoring nested brackets
- References:
- regexp: ignoring nested brackets
- From: Kaveh
- Re: regexp: ignoring nested brackets
- From: MSG
- Re: regexp: ignoring nested brackets
- From: Anno Siegel
- Re: regexp: ignoring nested brackets
- From: robic0
- Re: regexp: ignoring nested brackets
- From: Xicheng
- Re: regexp: ignoring nested brackets
- From: robic0
- regexp: ignoring nested brackets
- Prev by Date: Re: regexp: ignoring nested brackets
- Next by Date: Re: regexp: ignoring nested brackets
- Previous by thread: Re: regexp: ignoring nested brackets
- Next by thread: Re: regexp: ignoring nested brackets
- Index(es):
Relevant Pages
|