2

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

wqyfavor
  • 519
  • 1
  • 4
  • 14

1 Answers1

2

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

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Hi, with '^' and '&' this regex is used to test if a whole text matches phone number. But I need to use this regex to extract all phone numbers from arbitrary text. I removed '^' and '$', and used "3.1415926,0.0000001%,99.999999%, 2352352352.553535454.54545455" to test the regex, it doesn't work. – wqyfavor Nov 08 '16 at 05:36
  • I'm not familiar with the flavor of regex you are using, or the application, but you could try `.*\+?((?!\.)\d){7,23}.*` which would match the pattern anywhere in the string, possibly multiple times. – Tim Biegeleisen Nov 08 '16 at 05:39