use 5.010; # ++ possessive, (?PARNO), (DEFINE) use strict; use warnings; use Test::More # tests => ?? + 1 # Test::NoWarnings adds 1 test 'no_plan' ; use Test::NoWarnings; my $empty_curly = qr{ { \s* } }xms; my $empty_square = qr{ \[ \s* \] }xms; my $not_empty = qr{ (?! $empty_curly | $empty_square) }xms; my $curly = qr{ $not_empty { (?: [^{}] ++ | $empty_curly | (?R) )+ } }xms; my $square = qr{ $not_empty \[ (?: [^\[\]]++ | $empty_square | (?R) )+ \] }xms; my $re1 = qr{ $curly | $square }xms; my $re2 = qr{ ( (?&SQUARE) | (?&CURLY) ) # works # (?(?&SQUARE)) | (?(?&CURLY)) # works # (?&SQUARE) | (?&CURLY) # no # (?: (?&SQUARE) | (?&CURLY) ) # no (?(DEFINE) (? \[ \s* \] ) (? { \s* } ) (? (?! (?&EMPTY_SQUARE) | (?&EMPTY_CURLY))) (? (?&NOT_EMPTY) \[ (?: [^\[\]]++ | (?&EMPTY_SQUARE) | (?R) )+ \] ) (? (?&NOT_EMPTY) { (?: [^{}] ++ | (?&EMPTY_CURLY) | (?R) )+ } ) ) }xms; VECTOR: for my $ar_vector ( [ '...?[](...$[] = [ USER_ENTITY_NAME ], text${} = { this is a test })...', '[ USER_ENTITY_NAME ]', '{ this is a test }', ], [ 'a[] = a[ ] = a[ ] = [ this is a [ test ] { test2 } ]', '[ this is a [ test ] { test2 } ]', ], [ 'a{} = a{ } = a{ } = { this is a { test } [ test2 ] }', '{ this is a { test } [ test2 ] }', ], [ '{ a { b [ {}c{} ] d } e } = [ f [ g { []h[] } i ] j ]', '{ a { b [ {}c{} ] d } e }', '[ f [ g { []h[] } i ] j ]', ], [ '{}[]{ {}[] { } [ ] }[ ]{ } - [ ]{ }[ []{} [ ] { } ]{}[]', '{ {}[] { } [ ] }', '[ []{} [ ] { } ]', ], ) { my ($string, @expected) = @$ar_vector; is_deeply [ $string =~ m{ $re1 }xmsg ], \@expected, # qq{} ; is_deeply [ grep defined, $string =~ m{ $re2 }xmsg ], \@expected, # qq{} ; } # end for VECTOR