Skip to content

Instantly share code, notes, and snippets.

@prestongarno
Last active January 24, 2018 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prestongarno/5c7fd0fd3f868b51a7ee3002e8bfaf0c to your computer and use it in GitHub Desktop.
Save prestongarno/5c7fd0fd3f868b51a7ee3002e8bfaf0c to your computer and use it in GitHub Desktop.
fun parsePhoneNumber(text: String): PhoneNumber {
val destructuredRegex = "([0-9]{3})-([0-9]{3})-([0-9]{4})".toRegex()
return destructuredRegex.matchEntire(text)
?.destructured
?.let { (areaCode, prefix, lineNumber) ->
PhoneNumber(areaCode.toInt(), prefix.toInt(), lineNumber.toInt())
}
?: throw IllegalArgumentException("Bad input '$text'")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment