<?xml version="1.0" encoding="windows-1252"?>
<node id="574763" title="Parsing using m//g" created="2006-09-25 11:48:09" updated="2006-09-25 07:48:09">
<type id="115">
perlquestion</type>
<author id="329777">
pbeckingham</author>
<data>
<field name="doctext">
&lt;p&gt;
Can someone help?  I have given myself the challenge of doing some simple parsing, but in a complex way.  Without focusing on why I choose to do this, can someone guide me towards a viable solution?
Given the following input:
&lt;code&gt;
name1=value1
           name2  = value2
&lt;/code&gt;

This code parses it:
&lt;code&gt;
    while (&lt;$input&gt;)
    {
      chomp;
      next if /^ \s* #/;
      next if /^ \s* $/;

      if (/^ \s* ([^=\s]+) \s* = \s* (.+) $/x)
      {
        # name is in $1, value is in $2
      }
    }
&lt;/code&gt;

That's not the question though.  The question is, how would I parse the following:
&lt;code&gt;
name1=value1
             name2 = value2
name3 = value3
   but wait, there is
 more
name4=
  value4
&lt;/code&gt;

With Perl that has the form:
&lt;code&gt;
    my $contents = do {local $/; &lt;$input&gt;};
    while ($contents =~ / ANSWER_HERE /msg)
    {
      # name is in $1, value is in $2
    }
&lt;/code&gt;
Specifically, I want to use the //g form, to iterate over the string, and not perform a line-by-line parse, as in the first example.  My attempts have thus far failed.  The closest I got (without success) was:

&lt;code&gt;
    my $contents = do {local $/; &lt;$input&gt;};
    my $name = qr/\s* [^=\s]+ \s*/x;
    while ($contents =~ /^ ($name) = \s* (.+) (?= ^ $name = | $ ) /msgx)
    {
      # name is in $1, value is in $2
    }
&lt;/code&gt;
&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-329777"&gt;
&lt;br /&gt;&lt;br /&gt;&lt;font size="-3"&gt;pbeckingham - typist, perishable vertebrate.&lt;/font&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
