Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Parsing using m//g

by ikegami (Patriarch)
on Sep 25, 2006 at 12:00 UTC ( [id://574768]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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

Ouputs

[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"); }

Replies are listed 'Best First'.
Re^2: Parsing using m//g
by pbeckingham (Parson) on Sep 25, 2006 at 12: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.
      and/or
      Simply change $ to \z.

      Update: Added the second (and better) option.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://574768]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.