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
  • 6

2 Answers2

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

Here is a Live Demo

Ashkan Mobayen Khiabani
  • 32,319
  • 30
  • 98
  • 161
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
  • 127,136
  • 16
  • 45
  • 63