title: Challenge: Regex-only tokenizer for shell-assignment-like config lines
body: <p>I asked the original question <a href="https://stackoverflow.com/questions/8637688/ruby-1-9-regular-expression-to-match-unquoted-key-value-assignment/8639482#8639482">here</a>, and got a practical response with mixed Ruby and Regular Expressions. Now, the purist in me wants know: <strong>Can</strong> this be done in regular expressions? My gut says it can. There's an ABNF floating around for bash 2.0, though it doesn't include string escapes.</p>
  
  <p><strong>The Spec</strong></p>
  
  <p>Given an input line that is either (1) a variable ("key") assignment from a bash-flavored script or (2) a key-value setting from a typical configuration file like <code>postgresql.conf</code>, this regex (or pair of regexen) should capture the key and value in such a way that I can use those captures to substitute a new value for that key.</p>
  
  <p>You may use a different regular expression for shell-flavored and config-flavored lines; the caller will know which to use.  </p>
  
  <p><strong>There will be a 50-point bounty here.</strong> I can't add a bounty for two days, so I won't accept an answer till then, but you can start answering immediately.  You earn points for:</p>
  
  <ul>
  <li>Readability (named capture groups, definitions via ?(DEFINE) or {0})</li>
  <li>Using a single regex instead of two</li>
  <li>Teaching me something about DFA</li>
  <li>Regex performance, if relevant</li>
  <li>Getting upvoted</li>
  <li>First to use a technique</li>
  </ul>
  
  <p><strong>Example:</strong></p>
  
  <p>Given the input</p>
  
  <pre><code>export RAILS_ENV=production
  </code></pre>
  
  <p>I should be able to write in Ruby:</p>
  
  <pre><code>match = THE_REGEX.match("export RAILS_ENV=production")
  newline = "export #{match[:key]}=#{match[:value]}"
  </code></pre>
  
  <p><strong>Test cases: shell style</strong></p>
  
  <pre><code>RAILS_ENV=development     # Don't forget to change this for TechCrunch
  HOSTNAME=`cat /etc/hostname`
  plist=`cat "/Applications/Sublim\`e Text 2.app/Content's/Info.plist"`
  
  # Optional bonus input: "#" present in the string
  FORMAT="  ##0.00 passe\`" #comment
  </code></pre>
  
  <p><strong>Test cases: config style</strong></p>
  
  <pre><code>listen_addresses = 127.0.0.1 #localhost only by default
  # listen_addresses = 0.0.0.0 commented out, should not match
  </code></pre>
  
  <p>For the purpose of this challenge, "regular expression" and "regex" mean the same thing and both can refer to any common flavor you like, though I prefer Ruby 1.9-compatible.</p>