What does {,2}|1 mean for example?
You should be looking at the parts separated by | – \d{,2} is a pattern, 1\d{2} is a pattern, etc. Here’s what they mean:
- \d{,2}– up to 2 digit characters, i.e. numbers from 0 to 99
- 1\d{2}– the digit 1 followed by 2 digits, i.e. numbers from 100 to 199
- 2[0-4]\d– 2, then a digit from 0 to 4, then a digit, i.e. numbers from 200 to 249
- 25[0-5]– 2, 5, and a digit from 0 to 5, i.e. numbers from 250 to 255
When you join them together with |, it’s the pattern matching any of those patterns, i.e. numbers from 0 to 255.
The \d{,2} pattern is a bit wrong because it also matches the empty string and allows a leading zero, which is inconsistent with the other patterns.
If you wanted to check whether an entire string matched the pattern, a correct version would probably be this:
octet = /\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]/
ip_regex = /\A#{octet}\.#{octet}\.#{octet}\.#{octet}\z/