0

Define the regular expression date, which only matches dates written as ten-character strings YYYY-MM-DD. For example, 2016-04-12 represents April 12 2016, whereas 2016-04-31 is not a valid date.

So far I have this:

 val date =  """([0-9]{4}-[0-9]{2}-[0-9]{2})"`"".r`

Right it, the 'days' section accepts numbers all the way past 31. So you can have 2016-01-34

I believe I know how to fix that, however my issue is fiuring how to match each month. Meaning, how do I know January is 31 days, feb is 29 (leap year included), etc..

jwvh
  • 50,871
  • 7
  • 38
  • 64
rocketman
  • 43
  • 7
  • 1
    Encoding the rules for leap years will require a rather ugly expression with a bunch of cases and repetition. Are you open to solutions that are not a single regular expression? And are you intentionally avoiding [`java.time`](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) and other libraries that will do date parsing for you? – Chris Martin Apr 13 '16 at 22:50
  • I'd rather have one regular expression and yes, my idea is to work with scala.util.matching.Regex only – rocketman Apr 13 '16 at 22:56
  • It's an interesting question, esp in the context of doing a pattern match. Something like `date match { case dated"([0-9]{4}-${ Month(...) }-${ Day(...) }"`. That's not pure regex. And that's instead of doing a match with a guard, `case dated"..." if month allows day =>`. – som-snytt Apr 13 '16 at 23:09
  • OK how about this formulation http://stackoverflow.com/questions/36615346/extractor-composition-with-dependent-sub-patterns – som-snytt Apr 14 '16 at 06:30

0 Answers0