Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Parsing using m//g

by ikegami (Pope)
on Sep 25, 2006 at 16:00 UTC ( #574768=note: print w/ replies, xml ) Need Help??

in reply to Parsing using m//g

my $contents = do { local $/; <DATA> }; while ($contents =~ / \s* ([^=\s]+) \s* = \s* ( (?: (?! \s* (?: [^=\s]+ \s* = | $ ) ) . )* ) /xmsg ) { print("[$1 => $2]\n"); } __DATA__ name1=value1 name2 = value2 name3 = value3 but wait, there is more name4= value4


[name1 => value1] [name2 => value2] [name3 => value3] [name4 => value4]

Update: The above works by never allowing bad data in the value. The following is an alternate solution that works by starting with an empty value, and extending it as much as possible.

my $contents = do { local $/; <DATA> }; while ($contents =~ / \s* ([^=\s]+) \s* = \s* (.*?) # Extend the value. (?= \s* (?: [^=\s]+ \s* = | $ ) ) /xmsg ) { print("[$1 => $2]\n"); }

Comment on Re: Parsing using m//g
Select or Download Code
Replies are listed 'Best First'.
Re^2: Parsing using m//g
by pbeckingham (Parson) on Sep 25, 2006 at 16:03 UTC

    To be correct, your output would have to be:

    [name1 => value1] [name2 => value2] [name3 => value3 but wait, there is more] [name4 => value4]

    pbeckingham - typist, perishable vertebrate.

      Simply change /.../xmsg to /.../xsg.
      Simply change $ to \z.

      Update: Added the second (and better) option.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2015-11-27 01:54 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (712 votes), past polls