-2

I am using below pattern in json schema to validate strings.

"pattern": "^(nfs://)(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):([0-9]{4})"

But currently it is not validating "nfs://172.1.1:2049" as invalid string.

tadman
  • 208,517
  • 23
  • 234
  • 262
Anuradha K
  • 21
  • 4

1 Answers1

1

This doesn't immediately seem like an obvious problem, but the . character needs to be escaped because you're trying to literally match that character.

This regex, with escaped . and forward slashes works:

^(nfs:\/\/)(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):([0-9]{4})

The problem was that since each capturing group that matches digits can match as few as one digit or as many as three, the regex engine looked at the first 1 (in 172), found that it was valid, then tried matching . (any character) and found the digit 7, which is not what you want.

In nfs://172.1.1:2049, the second capturing group in your regex matched the first 1 in the IP address, the . matched the 7, the third capturing group matched the 2.. and so on.

Try it here: https://regex101.com/r/TNXDiQ/1

omijn
  • 646
  • 1
  • 4
  • 11