-1

Need regex for 6 or 10 digit number can be start and end with space, can also be a single word in a whole string. It should not start and end with '/'.

For example:

  1. My Zip-code is 101010. Please update. -> 101010
  2. 9090909090 is my mobile number -> 9090909090
  3. 00/9090909090/000000/ -> should not find any number
  4. 9090909090 -> 9090909090

I have tried this

\b(\d{11}|\d{10}|\d{6})\b

regex, but unable to handle 3rd situation.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

1

You can use

\b\d{6}(?:\d{4})?\b(?!\/)

See the regex demo and the regex graph:

enter image description here

Details:

  • \b - a word boundary
  • \d{6} - any six digits
  • (?:\d{4})? - an optional sequence of four digits
  • \b - a word boundary
  • (?!\/) - there must be no / immediately to the right of the current location (this is a negative lookahead).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563