regex - How to find the next unbalanced brace? -
the regex below captures last balanced }
.
now, regex able capture next unbalanced }
? in other words, how can can ... {three {four}} five}
$str
instead of ... {three {four}}
?
my $str = "one 2 {three {four}} five} six"; if ( $str =~ / ( .*? { (?> [^{}] | (?-1) )+ } ) /sx ) { print "$1\n"; }
so want match
[noncurlies [block noncurlies [...]]] "}"
where block
is
"{" [noncurlies [block noncurlies [...]]] "}"
as grammar:
start : text "}" text : noncurly* ( block noncurly* )* block : "{" text "}" noncurly : /[^{}]/
as regex (5.10+):
/ ^ ( ( [^{}]* (?: \{ (?-1) \} [^{}]* )* ) \} ) /x
as regex (5.10+):
/ ^ ( (?&text) \} ) (?(define) (?<text> [^{}]* (?: (?&block) [^{}]* )* ) (?<block> \{ (?&text) \} ) ) /x
Comments
Post a Comment