title: Parsing attributes with regex in Perl
body: <p>Here's a problem I ran into recently.  I have attributes strings of the form</p>
  
  <pre><code>"x=1 and y=abc and z=c4g and ..."
  </code></pre>
  
  <p>Some attributes have numeric values, some have alpha values, some have mixed, some have dates, etc.</p>
  
  <p>Every string is <em>supposed</em> to have "<code>x=someval and y=anotherval</code>" at the beginning, but some don't.  I have three things I need to do.  </p>
  
  <ol>
  <li>Validate the strings to be certain that they have <code>x</code> and <code>y</code>.  </li>
  <li>Actually parse the values for <code>x</code> and <code>y</code>.</li>
  <li>Get the rest of the string.</li>
  </ol>
  
  <p>Given the example at the top, this would result in the following variables:</p>
  
  <pre><code>$x = 1;
  $y = "abc";
  $remainder = "z=c4g and ..."
  </code></pre>
  
  <p>My question is: Is there a (reasonably) simple way to parse these <em>and</em> validate with a single regular expression?  i.e.:</p>
  
  <pre><code>if ($str =~ /someexpression/)
  {
      $x = $1;
      $y = $2;
      $remainder = $3;
  }
  </code></pre>
  
  <p>Note that the string may consist of <em>only</em> <code>x</code> and <code>y</code> attributes.  This is a valid string.</p>
  
  <p>I'll post my solution as an answer, but it doesn't meet my single-regex preference.</p>