Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It's a matter of reliably recognizing keys from values. There are two things that complicate matters:
  • Values are optional;
  • Values can contain "=" signs, in particular in URLs.

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.

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"; } }
Result:
$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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found