feetslashbirds has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I've got a very long list of data that I need to parse but I'm unable to get my regex to stop being greedy. The data is formatted as follows... [[{bunches of data}, {more data}, {etc}], [{hey more data}, {etc}, {etc}], [{etc}, {etc}, {etc}]] and my regex looks like this,
#counter is equal to the number of [] pairs while($counter) { if($content =~ /\[{(.*)?}\](.+)/) { print OUT "$counter\n"; print OUT "$1\n"; $content = $2; } #}]], "plt": --$counter; }
The idea is to get the regex to grab each [] pair one at a time but instead the first pass grabs everything up to the ending }]] Help me perl monks, you are my only hope

Replies are listed 'Best First'.
Re: Non Greedy?
by zwon (Abbot) on Jun 02, 2009 at 21:48 UTC
    $content =~ /\[{(.*?)}\](.+)/

    Update: also you can rewrite your loop as follows:

    while ( $content =~ /\[{(.*?)}\]/g ) { print "$1\n"; }
    note, that this doesn't use counter and just prints all occurrences.
      gracias, I feel like a dummy
Re: Non Greedy?
by ikegami (Patriarch) on Jun 02, 2009 at 21:53 UTC

    Your "?" is modifying "()" instead of "*". You want

    / \[{ ( .*? ) }\] /x
    or
    / \[{ ( [^\]]* ) }\] /x

    Note that your strategy only works if all the content is two-level deep.

Re: Non Greedy?
by carlin (Beadle) on Jun 02, 2009 at 21:51 UTC
    Should

    /\[{(.*)?}\](.+)/

    not be

    /\[{(.*?)}\](.+)/

    With the question mark inside the brackets?
Re: Non Greedy?
by citromatik (Curate) on Jun 02, 2009 at 23:54 UTC

    Some gave you hints to solve your regexp problem. I will give you a quick-n-dirty non-regexp related solution (I will never put a code like this in production):

    use strict; use warnings; use Data::Dumper; my $i = "[[{bunches_of_data}, {more_data}, {etc}], [{hey_more_data}, { +etc}, {etc}], [{etc}, {etc}, {etc}]]"; $i =~ s/{|}//g; $i =~ s/([^\[\],\s]+)/'$1'/g; my $iv = eval "$i"; print Dumper $iv;

    Outputs:

    $VAR1 = [ [ 'bunches_of_data', 'more_data', 'etc' ], [ 'hey_more_data', 'etc', 'etc' ], [ 'etc', 'etc', 'etc' ] ];

    citromatik

Re: Non Greedy?
by Devasundaram (Initiate) on Jun 03, 2009 at 07:50 UTC