in reply to parse string containing space

One possible way:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $s = q{name1=value1 name2=' value2=0' name3=value3}; ;; my $name = qr{ \w+ }xms; my $plain = qr{ \w+ }xms; my $s_quoted = qr{ ' [^\x27]* ' }xms; ;; my %h = $s =~ m{ ($name) \s* = \s* ($s_quoted | $plain) \s* }xmsg; dd \%h; " { name1 => "value1", name2 => "' value2=0'", name3 => "value3" }
Please see perlre, perlrequick, and perlretut.

Updates:

  1. Note that  \x27 in  [^\x27] represents a  ' (single-quote) character. I have to use this form because my REPL does not like unbalanced single-quotes in a command-line code expression. You can use  [^'] like a sane person.
  2. Another and probably better approach would be to forget regexes and use Text::CSV or Text::CSV_XS.
  3. If you need to get rid of the single-quotes and have Perl version 5.10+, try this. Note that  $s_quoted is changed and the  m// match uses  (?|pattern) from Extended Patterns.
    c:\@Work\Perl>perl -wMstrict -MData::Dump -le "use 5.010; ;; my $s = q{name1=value1 name2=' value2=0' name3=value3}; ;; my $name = qr{ \w+ }xms; my $plain = qr{ \w+ }xms; my $s_quoted = qr{ [^\x27]* }xms; ;; my %h = $s =~ m{ ($name) \s* = \s* (?| ' ($s_quoted) ' | ($plain)) \s +* }xmsg; dd \%h; " { name1 => "value1", name2 => " value2=0", name3 => "value3" }
    If you don't have 5.10, let me know. There's a simple alternative.


Give a man a fish:  <%-(-(-(-<