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