Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Parsing "=" separated output

by Ratazong (Prior)
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:

(?=\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


Comment on Re: Parsing "=" separated output
Select or Download Code
Re^2: Parsing "=" separated output
by blindluke (Pilgrim) 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 (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.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://918183]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-11-23 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls