wqyfavor
wqyfavor

Reputation: 539

Regular Expression for Phone Number and excludes decimal numbers

I use this regex to recognize phone numbers in my app. "\\+?\\d{7,23}" But this cannot exclude decimal numbers like 3.1415926。

How to modify this regex, so that it can recognize phone numbers and do not give me decimal numbers like 3.1415926, 99.9999999。

In this case, the '1415926' and '9999999' will be recognized as phone number, which is not desired.

In a word, I want to reject numbers with '.' to be partially recognized as phone number. Or a phone number should not be succeeded with '.' or follow '.'.

Thanks.

Finally, I solved this problem using enter image description here

Upvotes: 2

Views: 94

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520938

Try this regex:

^\+?((?!\.)\d){7,23}$

Explanation:

\+                string starts with an optional +
((?!\.)\d){7,23}  negative lookahead asserts that string contains between 7 and 23
                  numbers, each of which is not a dot

Demo here:

Regex101

Upvotes: 2

Related Questions