I have a year, expressed in the format XXYY-ZZ. For example, the year 2020-21 would represent a year spanning 2020 to 2021. I need to extract XXYY, YY and ZZ as Ints to use in calculations later.
Using Pattern matching and regex, I can extract values I want as Strings, like this:
import scala.util.matching.Regex
val YearFormatRegex: Regex = "(20([1-9][0-9]))-([1-9][0-9])".r
"2020-21" match {
case YearFormatRegex(fullStartYear, start, end) => println(fullStartYear, start, end)
case _ => println("did not match")
}
// will print (2020, 20, 21)
However I need the values as Ints. Is there a way to extract these values as Ints without throwing .toInt all over the place? I understand that the regex specifically looks for numbers so extracting them as Strings and then parsing as Ints seems like an unnecessary step if I can avoid it.