I am working on a validation with Regex and the condition is below
1(061)235-663 - valid
1(061)235-663 - valid
(061)235-663 - valid
The regex I am trying is below
val regex = """\d{3}([-.])\d{3}\1\d{4}|\d{10}|^(\ 1|1)?(\(\d{3}\)\d{3}\-\d{3})$""".toRegex()
our focus is on last part : ^(\ 1|1)?(\(\d{3}\)\d{3}\-\d{3})
I am not able to escape 1. other cases are working. So the scenario is like that, 1 and 1 is optional.
It seems like back slash not working as escape character in kotlin Regex
CodePudding user response:
not sure what you need to catch, but if you add a second group, you will only match the number (061)235-663
^(\ 1|1)?(\(\d{3}\)\d{3}\-\d{3})
CodePudding user response:
^[ |(|\d]?[\d|(|\s|] [)|\s] \d [-|\s] \d
logic used is
- starting with or "(" or a digit
- second char is "(" or space or digit. it can be of any length, if you need 3 u can limit
- next stop is either at ")" or space
- then followed by numbers of any length
- then followed by "-" or space
- then followed by digits.
these can be used just as direction to what finally you need. https://regex101.com/r/GKlnoz/1