Kristian
Kristian

Reputation: 1378

regex not matching number correctly

I have the following regex: (\d{14}) decimal that matches 14 character long number. The problem is that it also matches numbers, that are 16 characters long. I need to add a condition to match if there are no numbers at beginning or end of string.

So for example 112222222222222233 wouldn't be a match i want, but xx22222222222222xx would be match I need.

Upvotes: 0

Views: 68

Answers (2)

ctn
ctn

Reputation: 2930

M42's answer can work in cases where the number is delimited by spaces or other word delimiters. But if you want to match a number in a word containing non-digits (like your example xx22222222222222xx) something like this should work:

(^|[^\d])\d{14}([^\d]|$)

Upvotes: 1

Toto
Toto

Reputation: 91385

use word boundary \b

\b\d{14}\b

Upvotes: 5

Related Questions