I personally would not use regex for this. I think itβs easier to make sure that the string consists of 9 digits, divide the string into 3 groups of 3-digit numbers, and then check that each number is between 0 and 255, inclusive.
If you really insist on regex, you can use something like this:
"([0-1][0-9][0-9]|2[0-4][0-9]|25[0-5]){3}"
The expression contains an alternation of three terms: first matches 000-199 , second 200-249 , third 250-255 . {3} requires matching exactly three times.
source share