Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, you can't do this. If you look at the definition of the Parser used when you convert a regex to a Parser, it throws away all context and just returns the full matched string:</p> <p><a href="http://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_7_7_final/src/library/scala/util/parsing/combinator/RegexParsers.scala?view=markup#L55" rel="nofollow noreferrer">http://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_7_7_final/src/library/scala/util/parsing/combinator/RegexParsers.scala?view=markup#L55</a></p> <p>You have a couple of other options, though:</p> <ul> <li>break up your parser into several smaller parsers (for the tokens you actually want to extract)</li> <li>define a custom parser that extracts the values you want and returns a domain object instead of a string</li> </ul> <p>The first would look like</p> <pre><code>val separator = "-" | "/" val year = ("""\d{4}"""r) &lt;~ separator val month = ("""\d\d"""r) &lt;~ separator val day = """\d\d"""r val date = ((year?) ~ (month?) ~ day) map { case year ~ month ~ day =&gt; (year.getOrElse("2009"), month.getOrElse("11"), day) } </code></pre> <p>The <code>&lt;~</code> means "require these two tokens together, but only give me the result of the first one.</p> <p>The <code>~</code> means "require these two tokens together and tie them together in a pattern-matchable ~ object.</p> <p>The <code>?</code> means that the parser is optional and will return an Option.</p> <p>The <code>.getOrElse</code> bit provides a default value for when the parser didn't define a value.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload