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 16:03 UTC | |
by ikegami (Patriarch) on Sep 25, 2006 at 16:09 UTC |