Would the kotlin equivalent just be:
val dateRegex = Regex("""(\d{4})-(\d{2})-(\d{2})""")
val (year, _, _) = dateRegex.matchEntire("2004-01-20")!!.destructured
println("$year was a good year for PLs.")
?
The Scala snippet works for all inputs, it's something you would see on a real codebase. It also scopes the regex captures within the match expression. The code also scales better for more complex cases where a string is tested against multiple regexes, or against specific captured values.
I assume this Kotlin snippet would, at minimum, need to do null-checking on "matchEntire" before it finds its way to a production codebase.
vvillena|11 months ago
I assume this Kotlin snippet would, at minimum, need to do null-checking on "matchEntire" before it finds its way to a production codebase.