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

In reply to Re^4: Regular expressions: Extracting certain text from a line by AnomalousMonk
in thread Regular expressions: Extracting certain text from a line by Wcool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.