object PhoneNumber {
private val VALID_PHONE_NUMBER = raw"^1?([2-9]\d{2}[2-9]\d{6})$$".r
def clean(input: String): Option[String] = {
val cleaned = input.filter(_.isDigit)
Option(cleaned) collect { case VALID_PHONE_NUMBER(cleaned) => cleaned }
}
}
This approach starts by defining the regex pattern for a valid phone number in a raw string.
-
^1?means that theStringbeing checked starts with1one or zero times. -
[2-9]means the next character must be2through9. -
\d{2}means the next two characters must be digits (with no limit to their value, so0through9.) -
[2-9]means the next character must be2through9. -
\d{6}means the next six characters must be digits (with no limit to their value, so0through9.) -
$$is the only escape in the raw string.$means the end of the string, so theStringbeing matched must have the preceeding six digits as the last part of theString.
The input String calls the filter() method.
Each character is passed to filter(), which in turn passes the character to the isDigit method.
Only the characters that are digits survive to be passed as a String to Option().
If there are no digits, then Option returns None.
Otherwise, the cleaned String is passed to collect().
The case returns the cleaned String if it matches the valid phone number pattern.
If the phone number does not match the valid pattern, then None is returned.
16th Aug 2024
·
Found it useful?