in reply to Re^3: Regular expressions: Extracting certain text from a line
in thread Regular expressions: Extracting certain text from a line
Hi Ken!
Here's my latest try. It may be of interest to you. This is full-on 5.10+ as I wanted to get away from the (??{ ... }) construct with its scary warnings and experiment some more with the (?PARNO) construct, and also with (DEFINE), which I still don't fully understand. As you see, the (DEFINE) version requires an extra grep step; I couldn't figure out how to avoid it. Also, definition of empty squares or curlies expanded to include unlimited whitespace. Tested under Strawberries 5.10.1.5 and 5.14.4.1.
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 # (?<X>(?&SQUARE)) | (?<Y>(?&CURLY)) # works # (?&SQUARE) | (?&CURLY) # no # (?: (?&SQUARE) | (?&CURLY) ) # no (?(DEFINE) (?<EMPTY_SQUARE> \[ \s* \] ) (?<EMPTY_CURLY> { \s* } ) (?<NOT_EMPTY> (?! (?&EMPTY_SQUARE) | (?&EMPTY_CURLY))) (?<SQUARE> (?&NOT_EMPTY) \[ (?: [^\[\]]++ | (?&EMPTY_SQUARE) | + (?R) )+ \] ) (?<CURLY> (?&NOT_EMPTY) { (?: [^{}] ++ | (?&EMPTY_CURLY) | + (?R) )+ } ) ) }xms; VECTOR: for my $ar_vector ( [ '...?[](...$[] = [ USER_ENTITY_NAME ], text${} = { this is a tes +t })...', '[ 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Regular expressions: Extracting certain text from a line
by kcott (Archbishop) on Apr 09, 2014 at 03:08 UTC | |
by AnomalousMonk (Archbishop) on Apr 09, 2014 at 07:44 UTC |