Now what distinguishes a key from a value, is that it starts at the start of a line, plus some whitespace; it contains only word characters, and is followed by (optional?) whitespace, and a "=" sign. If the whitespace isn't optional, it's easier to prevent false positives for recognized keys, because an URL can't contain whitespace. Still, you can't get an URL containing a "=" character while not containing any other non-word characters on the left of it, in particular any of colons, slashes and question marks.
My preference would go to a solution that involves split. If split gets a regexp that contains capturing parens, the captured values will be inserted between the split data. So I split the values (possibly empty), and capture the keys. You'll get a leading dummy "value", without associated key, so we'll have top get rid of that first.
This is code that works with the two versions of the input, but the regexp might be tweaked to minimize the chance of either false positives, and false negatives.
Result:foreach (<<'-1-', <<'-2-') { <template> foo = Yes bar = 0 blort = home_url = http://www.foo.com/some/really/long/url </template> -1- <template> foo = Yes bar = 0 blort = home_url = http://www.foo.com/some/really/long/url </template> -2- if(m[^<template>(.*?)\s*^</template>]sm) { (undef, my %data) = split /\s*^\s*(\w+)\s+=[^\S\n]*\n?/m, $1, +-1; use Data::Dumper; print Dumper \%data; } else { print "No match\n"; } }
$VAR1 = { 'blort' => '', 'foo' => 'Yes', 'bar' => '0', 'home_url' => 'http://www.foo.com/some/really/long/url' }; $VAR1 = { 'blort' => '', 'foo' => 'Yes', 'bar' => '0', 'home_url' => 'http://www.foo.com/some/really/long/url' };
In reply to Re: Unwrapping values in a template
by bart
in thread Unwrapping values in a template
by hacker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |