blindluke has asked for the wisdom of the Perl Monks concerning the following question:
Hello, enlightened Monks!
I have a string, that looks a bit like this:
"sometrash key1=value0 value1, value2 key2=value3 key3=value4"
I want to extract keys and values to a hash, like this one:
{ key1 => 'value0 value1, value2', key2 => 'value3', key3 => 'value4', }
I used something like the code below, but for key1 it only gives me 'value0' - I thought there wouldn't be any whitespace in the values, and I was wrong :)
my %data; while ($string =~ m/\s+([^\s]+?)=([^\s]*)/g) { $data{$1} = $2; }
How can I modify my regexp, so that it only treats a single word right before the '=' sign as the key, and all the characters up until next key (or end of string) are treated as the value?
Regards, Luke Jefferson
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing "=" separated output
by Ratazong (Monsignor) on Aug 03, 2011 at 07:14 UTC | |
by blindluke (Hermit) on Aug 03, 2011 at 07:43 UTC | |
by Utilitarian (Vicar) on Aug 03, 2011 at 08:16 UTC | |
by johngg (Canon) on Aug 03, 2011 at 09:46 UTC | |
|
Re: Parsing "=" separated output
by egga (Monk) on Aug 03, 2011 at 07:16 UTC | |
by Anonymous Monk on Aug 03, 2011 at 10:37 UTC | |
by Anonymous Monk on Aug 03, 2011 at 10:39 UTC |