By default, a Regex created with the .r method is anchored, which means it must match the whole string. (think of your regex as being enclosed in ^ and $ )
You could use an unanchored regex instead:
def parser(filename: String):String = {
val extractDate = """(\d{8})""".r.unanchored
val extractDate(dd) = filename
dd
}
This works, but is bad practice since "parsing" would throw an exception if your input does not match. More idiomatic would be to return an Option[String] and handle that at the calling site. For example:
def parser(filename: String): Option[String] = {
"""\d{8}""".r.findFirstIn(filename)
}
parser("test_pb_PP_Quality_2-Report_20200707.csv") match {
case Some(datetime) => // do something
case None => // handle this case
}