in reply to Regular expressions: Extracting certain text from a line

G'day Wcool,

You get the output:

MATCH = [](EntityName$ = DERIVED_ATTRIBUTE_TABLE, FieldNames$[]

because

  1. Regex '\[' matches character in line: '['.
  2. Regex '.+?' matches at least one character (']') and then continues to match non-greedily (i.e. up to but not including the next ']'), that's '(EntityName$ = DERIVED_ATTRIBUTE_TABLE, FieldNames$['.
  3. Regex '\]' matches the ']' after that.

So, instead of matching any character one or more times non-greedily (i.e. '.+?'), what you really want is to match any character that isn't ']' one or more times greedily (i.e. '[^\]]+').

You get the ouput:

MATCH = {} = { this is a test }

for much the same reasons. The fix is similar, changing '.+?' to '[^}]+'.

Also, unless you really want those extra captures, you can lose the two inner pairs of parentheses.

Here's my test:

#!/usr/bin/env perl -l use strict; use warnings; my $line = 'EntityMappingFetchByName?[](EntityName$ = DERIVED_ATTRIBUT +E_TABLE, FieldNames$[] = [ USER_ENTITY_NAME ], text${} = { this is a +test }), line 6'; my $re = qr< ( { [^}]+ } | \[ [^\]]+ \] ) >x; print "MATCH = $1" while $line =~ /$re/g;

Output:

MATCH = [ USER_ENTITY_NAME ] MATCH = { this is a test }

-- Ken

Replies are listed 'Best First'.
Re^2: Regular expressions: Extracting certain text from a line
by AnomalousMonk (Archbishop) on Apr 08, 2014 at 06:40 UTC

      Grrr! I wish they wouldn't do that.

      Anticipating more ante upping, with deeply nested brace/bracket combos and wanting to capture a nested (but not an isolated) '{}' or '[]', e.g. '{ {} }', here's (maybe) a bit of a cheat:

      #!/usr/bin/env perl -l use strict; use warnings; my ($brace_re, $bracket_re); $brace_re = qr< { (?: [^{}]++ | (??{ $brace_re }) )* } >x; $bracket_re = qr< \[ (?: [^\[\]]++ | (??{ $bracket_re }) )* \] >x; my $re = qr< ( $brace_re | $bracket_re ) >x; while (<DATA>) { print; while (/$re/g) { print "MATCH = $1" if length $1 > 2; } print '-' x 60; } __DATA__ ...?[](...$[] = [ USER_ENTITY_NAME ], text${} = { this is a test })... a[] = [ this is a [ test ] { test2 } ] a{} = { this is a { test } [ test2 ] } { a { b [ {}c{} ] d } e } = [ f [ g { []h[] } i ] j ] {}[]{ {}[] }[]{} - []{}[ []{} ]{}[]

      Output:

      ...?[](...$[] = [ USER_ENTITY_NAME ], text${} = { this is a test })... MATCH = [ USER_ENTITY_NAME ] MATCH = { this is a test } ------------------------------------------------------------ a[] = [ this is a [ test ] { test2 } ] MATCH = [ this is a [ test ] { test2 } ] ------------------------------------------------------------ a{} = { this is a { test } [ test2 ] } MATCH = { this is a { test } [ test2 ] } ------------------------------------------------------------ { a { b [ {}c{} ] d } e } = [ f [ g { []h[] } i ] j ] MATCH = { a { b [ {}c{} ] d } e } MATCH = [ f [ g { []h[] } i ] j ] ------------------------------------------------------------ {}[]{ {}[] }[]{} - []{}[ []{} ]{}[] MATCH = { {}[] } MATCH = [ []{} ] ------------------------------------------------------------

      Update: For Perl v5.8, you'll need to change [...]++ to (?> [...]+ ) (the '++' appeared in v5.10) and qr<...> delimiters will need to be something else, e.g. qr!...!.

      The '(??{ $re })' construct has been around since at least v5.8.8.

      Here's the perlre doco for 5.8.8 and 5.10.0.

      -- Ken

        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.

        Grrr! I wish they wouldn't do that.

        Yeah, it's much too much like those casual "Oh, by the way..." comments on the way out of a meeting at work that drive a steel spike into the heart of a careful proposal you've just finished presenting to apparent general approval. I don't hang around this place just so I can get more of what I get at work.