Skip to content

Instantly share code, notes, and snippets.

@avgprog
Created February 8, 2017 04:57
Show Gist options
  • Save avgprog/0b798d1cefc59192d2d1bc1106e642e4 to your computer and use it in GitHub Desktop.
Save avgprog/0b798d1cefc59192d2d1bc1106e642e4 to your computer and use it in GitHub Desktop.
Scala custom datatype detection using regex parser
object CustomTypeInference {
def main(args: Array[String]) = {
val input_string_list = List("22-12-2016","2.334e10","2.345","-1234","-1","1.e10","25/12/16","21-21-16","21/21/2016")
// Date format of the type can be detected: DD-MM-YYYY | DD-MM-YY | DD/MM/YYYY | DD/MM/YY
val dateRegex = """([0-9]{2}-[0-9]{2}-[0-9]{4}|[0-9]{2}-[0-9]{2}-[0-9]{2}|[0-9]{2}/[0-9]{2}/[0-9]{4}|[0-9]{2}/[0-9]{2}/[0-9]{2})"""
val timeRegex = """([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3})"""
//Numerical datatype
val numRegex = """(\-?[0-9]*\.?[0-9]*e?[0-9]*)"""
val NumberFmt = numRegex.r
val DateFmt = dateRegex.r
val DateTimeFmt = (dateRegex + " " + timeRegex).r
def get(s: String) = s match {
case DateFmt(d) => "Date"
case DateTimeFmt(d,t) => "DateTime"
case NumberFmt(n) => "Number"
case _ => "String"
}
input_string_list foreach { x =>
println(get(x))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment