http://www.perlmonks.org?node_id=918183


in reply to Parsing "=" separated output

Hi Luke!

I assume your difficulty is to determine when the data ends and when the next key is coming. You could solve it with a look-ahead:

(?=\s[^\s]+=)

This looks for a blank and then some word followed by an = - according to your example that is your next key.

Unfortunately, with the look-ahead you would miss the last entry in your example, as there is no next key. But that can be solved easily by adding some dummy extension to the original string. So the following code works for me:

my $string = "sometrash key1=value0 value1, value2 key2=value3 key3=v +alue4"; $string .= " dummy="; # add a dummy value for the rege +x to work while ($string =~ m/\s+([^\s]+?)=(.*?)(?=\s[^\s]+=)/g) { $data{$1} = $2; }
HTH, Rata

PS.: please also read the node Death to Dot Star! to understand the risk in my altered solution

Replies are listed 'Best First'.
Re^2: Parsing "=" separated output
by blindluke (Hermit) on Aug 03, 2011 at 07:43 UTC

    Thanks!

    I've modified the regexp a bit, so that the concatenation is no longer necessary

    while ( $string =~ m/\s+([^\s]+?)=(.*?)(?=\s[^\s]+=|$)/g )

    Works quite well.

    Regards,
    Luke

      Same idea but taking advantage of the wantarray response of a match and commenting the regex
      ~/$ perl -e ' $string="sometrash key1=value0 value1, value2 key2=value3 key3=value4" +; %hash = $string =~/ (\w+) #capture the key name = # separated from the value by an equals (.+?) # and then the value, non-greediness prevents running + into the subsequent values (?=(?:\s\w+=|$)) # finally we look ahead to ensure that what follows i +s a space and a "key=" pattern, or else the end end of the string /xg; #Match globally and allow comments in the regex for m +aintainability # And check use Data::Dumper;print Dumper(\%hash); ' $VAR1 = { 'key2' => 'value3', 'key1' => 'value0 value1, value2', 'key3' => 'value4' };
      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re^2: Parsing "=" separated output
by johngg (Canon) on Aug 03, 2011 at 09:46 UTC
    Unfortunately, with the look-ahead you would miss the last entry in your example

    Just put an alternation in the look-ahead to cope with end of string.

    knoppix@Microknoppix:~$ perl -MData::Dumper -Mstrict -wE ' > my $str = > q{sometrash key1=value0 value1, value2 key2=value3 key3=value4}; > my %data = $str =~ m{([^\s=]+)=(.*?)(?=(?:\s+[^\s=]+=|\z))}g; > print Data::Dumper->Dumpxs( [ \ %data ], [ qw{ *data } ] );' %data = ( 'key2' => 'value3', 'key1' => 'value0 value1, value2', 'key3' => 'value4' ); knoppix@Microknoppix:~$

    I hope this is of interest.

    Update: I just realised this is in essence exactly the same as Utilitarian's reply. Please ignore.

    Cheers,

    JohnGG