0

I'm trying to match only {n} digits not more or less and may be surrounded by characters or special symbols

example:
suppose the {n} = {14}

*12345678901234*300 OK

12345678901234x21 OK

*123456789012345*300 NOT OK

12345678901234 OK

123456789012345 NOT OK

Fady Huwaidy
  • 41
  • 1
  • 7

2 Answers2

1
(?:^|\D)(\d{14})(?:\D|$)

Here is a Live Demo

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
1

You could use negative lookarounds to assert what is directly on the left and right is not a digit and match 14 digits:

(?<!\d)\d{14}(?!\d)

.NET regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70