Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Parsing "=" separated output

by Ratazong (Monsignor)
on Aug 03, 2011 at 07:14 UTC ( #918183=note: print w/replies, xml ) Need Help??

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:


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


    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.


      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 (Abbot) 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.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://918183]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-03-19 00:28 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (231 votes). Check out past polls.