java - extract date from string in Scala efficiently -
i want extract date (e.g. 2015-01-01
) multiple strings have form in scala:
val s = "basedir/somedir/tmp/blah/2015-01-01.txt"
i know can basic string split-trim-strip operations achieve that, there more cleaner way in scala? can use nice regex "hidden features" scala offers this?
i tried without success:
val s = "basedir/somedir/tmp/blah/2015-01-01.txt" val regex = "(\\d+)-(\\d+)-(\\d+).txt" val regex(year, month, date) = s
use pattern matching using regex extractor
val regex = ".*/(\\d{4}-\\d{2}-\\d{2}).txt".r //remove / after .* if think not needed. str match { case regex(date) => some(date) case _ => none }
use above code instead of below because below 1 cause match error @ runtime.
val regex(a) = "basedir/somedir/tmp/blah/2015-01-01.txt"
instead of .*
in front of regex. can use unanchored
.
val regex = "(\\d{4}-\\d{2}-\\d{2}).txt".r.unanchored
scala repl
scala> val regex = "(\\d{4}-\\d{2}-\\d{2}).txt".r.unanchored regex: scala.util.matching.unanchoredregex = (\d{4}-\d{2}-\d{2}).txt scala> val regex(a) = "basedir/somedir/tmp/blah/2015-01-01.txt" a: string = 2015-01-01
scala repl
scala> val regex = ".*/(\\d{4}-\\d{2}-\\d{2}).txt".r regex: scala.util.matching.regex = .*/(\d{4}-\d{2}-\d{2}).txt scala> val regex(a) = "basedir/somedir/tmp/blah/2015-01-01.txt" a: string = 2015-01-01
scala repl
scala> val str = "basedir/somedir/tmp/blah/2015-01-01.txt" str: string = basedir/somedir/tmp/blah/2015-01-01.txt scala> val regex = ".*/(\\d{4}-\\d{2}-\\d{2}).txt".r regex: scala.util.matching.regex = .*/(\d{4}-\d{2}-\d{2}).txt scala> | str match { | case regex(date) => some(date) | case _ => none | } res21: option[string] = some(2015-01-01)
if want match directory then
scala val regex = ".*/(.*)/(\\d{4}-\\d{2}-\\d{2}).txt".r regex: scala.util.matching.regex = .*/(.*)/(\d{4}-\d{2}-\d{2}).txt scala> val s = "basedir/somedir/tmp/blah/2015-01-01.txt" s: string = "basedir/somedir/tmp/blah/2015-01-01.txt" scala> val regex(dir, date) = s dir: string = "blah" date: string = "2015-01-01"
Comments
Post a Comment