title: parsing key/value pairs from string
body: <p>I'm parsing the body text from incoming emails, looking for key/value pairs.</p>
  
  <p><strong>Example Email Body</strong><br>
  First Name: John<br>
  Last Name:Smith<br>
  Email : john@example.com<br>
  Comments = Just a test comment that<br>
  may span multiple lines.</p>
  
  <p>I tried using a RegEx <code>([\w\d\s]+)\s?[=|:]\s?(.+)</code> in multiline mode. This works for most emails, but fails when there's a line break that should be part of the <strong>value</strong>. I don't know enough about RegEx to go any further.</p>
  
  <p>I have another parser that goes line-by-line looking for the key/value pairs and simply folds a line into the last matched value if a key/value pair is NOT found. It's implemented in Scala.</p>
  
  <pre><code>val lines = text.split("\\r?\\n").toList
  var lastLabelled: Int = -1
  val linesBuffer = mutable.ListBuffer[(String, String)]()
  
  // only parse lines until the first blank line
  // null_? method is checks for empty strings and nulls
  lines.takeWhile(!_.null_?).foreach(line =&gt; {
    line.splitAt(delimiter) match {
      case Nil if line.nonEmpty =&gt; {
        val l = linesBuffer(lastLabelled)
        linesBuffer(lastLabelled) = (l._1, l._2 + "\n" + line)
      }
      case pair :: Nil =&gt; {
        lastLabelled = linesBuffer.length
        linesBuffer += pair
      }
      case _ =&gt; // skip this line
    }
  })
  </code></pre>
  
  <p>I'm trying to use RegEx so that I can save the parser to the db and change it on a per-sender basis at runtime (implement different parsers for different senders).</p>
  
  <p>Can my RegEx be modified to match values that contain newlines?
  Do I need to just forget about using RegEx and use some JavaScript? I already have a JavaScript parser that lets me store the JS in the DB and essentially do everything that I <strong><em>want</em></strong> to do with the RegEx parser.</p>