1

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.

2
  • 1
    Regex is only operating on strings. You need to cast the extracted strings to int sooner or later if you need that type of data. Commented Sep 1, 2020 at 13:41
  • @Wiktor I understand that, I suppose I was hoping that there would be a convenient way to cast it to Int as it was matching, since I can already extract the values using the above method and in order to match in the first place they need to be numbers. Commented Sep 1, 2020 at 13:48

1 Answer 1

2

If you want to simply encapsulate the conversion, one way to do it could be to create your own extractor object built around your regular expression, e.g.:

import scala.util.matching.Regex

object Year {
  
  private val regex: Regex = "(20([1-9][0-9]))-([1-9][0-9])".r
  
  def unapply(s: String): Option[(Int, Int, Int)] =
    s match {
      case regex(prefix, from, to) => Some((prefix.toInt, from.toInt, to.toInt))
      case _ => None
    }
  
  
}

"2020-21" match {
  case Year(fullStartYear, start, end) => fullStartYear - start + end
  case _ => 0
} // returns 2020 - 20 + 21 = 2021

You can read more on extractor objects here on the Scala official documentation.

You can play around with this code here on Scastie.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.