Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: string parsing with split

by AnomalousMonk (Archbishop)
on Jan 24, 2011 at 22:27 UTC ( [id://884014]=note: print w/replies, xml ) Need Help??


in reply to string parsing with split

Here's my take on the problem. It has the advantage, IMHO, of being more easily adaptable to changing requirements because it is more modular.

Notes:

  • The regex uses  \x22 in place of  " (double-quote) to avoid Windoze command-line escape-ology.
  • A quoted string cannot contain any sort of double-quote, escaped or otherwise.
  • A parenthetic group cannot contain a  ')' (right-paren).
(Sorry for the line-wrap.)

>perl -wMstrict -le "my $s = 'key1=val1 key2=val2 key3=val3 key4=\"val4a val4b\" ' . 'key5=\"val5key=(0 1 2 3)\" key6=(val6a val6b)' ; ;; my $key = qr{ [[:alpha:]] [[:alnum:]]+ }xms; my $val = qr{ [[:alpha:]] [[:alnum:]]+ }xms; my $d_quo = qr{ \x22 [^\x22]* \x22 }xms; my $paren = qr{ [(] [^)]* [)] }xms; ;; my $vals = qr{ $val | $d_quo | $paren }xms; ;; my @opts = $s =~ m{ $key \s* = \s* $vals }xmsg; ;; print qq{'$s'}; print qq{'$_'} for @opts; " 'key1=val1 key2=val2 key3=val3 key4="val4a val4b" key5="val5key=(0 1 2 + 3)" key6=(val6a val6b)' 'key1=val1' 'key2=val2' 'key3=val3' 'key4="val4a val4b"' 'key5="val5key=(0 1 2 3)"' 'key6=(val6a val6b)'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found