5 ms·
The visualization tool shows that the regex is not correct. It allows 000.000.000.000 as an IPv4 address
by dmayle 9y ago
The visualization tool shows that the regex is not correct. It allows 000.000.000.000 as an IPv4 address
- grok2 9y agoI created this instead: https://regexper.com/#((%5B0-9%5D%5C.)%7C(%5B1-9%5D%5B0-9%5D%5C.)%7C(1%5B0-9%5D%5B0-9%5D%5C.)%7C(2%5B0-5%5D%5B0-5%5D%5C.))%7B3%7D((%5B0-9%5D)%7C(%5B1-9%5D%5B0-9%5D)%7C(1%5B0-9%5D%5B0-9%5D)%7C(2%5B0-5%5D%5B0-5%5D))(%24) https://regexper.com/#((%5B0-9%5D%5C.)%7C(%5B1-9%5D%5B0-9%5D... The repetition count seems to be displayed off-by-one though.
- reificator 9y agoOn mobile so can't (easily) test it, but doesn't this produce a false negative for `246.{snip}`, for example?
- grok2 9y agoYes, you are right...the following better? https://regexper.com/#((%5B0-9%5D%5C.)%7C(%5B1-9%5D%5B0-9%5D%5C.)%7C(1%5B0-9%5D%5B0-9%5D%5C.)%7C(2%5B0-4%5D%5B0-9%5D%5C.)%7C(25%5B0-5%5D%5C.))%7B3%7D((%5B0-9%5D)%7C(%5B1-9%5D%5B0-9%5D)%7C(1%5B0-9%5D%5B0-9%5D)%7C(2%5B0-4%5D%5B0-9%5D)%7C(25%5B0-5%5D))(%24) https://regexper.com/#((%5B0-9%5D%5C.)%7C(%5B1-9%5D%5B0-9%5D...
- emj 9y agoDoes not match: 10.1 -> 10.0.0.1
- grok2 9y agoIn IPv4 addresses, as far as I know, this is not done -- it is only IPv6 addresses that use the double colon to indicate a sequence of zeros.
- tzs 9y agoWhere are you seeing a double colon? He write "10.1", not "10::1". IPv4 addresses, according to POSIX, can be written in 4 forms: A1.A2.A3.A4 A1.A2.B A1.C D where Ai is an 8 bit number, B is a 16 bit number, C is a 24 bit number, and D is a 32 bit number. "10.1" is A1.C form. See the inet_addr man page if you are on Unix or a Unix-like system.
- grok2 9y agoI referred to the double colon as something I saw used with IPv6 to indicate a sequence of 0's. I wasn't aware that inet_aton supported all these forms and the regex I provide won't parse them. It seems like inet_aton supports specifying the numbers in octal and hex too.
- emj 9y agoGreat explanation thank you! The 24 bit and 16 bit variants are highly useable but they are often overlooked by regexps, biggest problem is web apps. Being able to use octal and hex is even less common.
- kazinator 9y agoI don't see the problem. Though not a host address, that's known in the sockets networking API as INADDR_ANY, useful for binding a socket to listening on all networks, for instance. The GNU C Library getaddrinfo accepts 000.000.000.000 with the leading zeros and all; I just tried. It is important to support special addresses like 255.255.255.255 and 0.0.0.0 in the dot notation. For instance, in the configuration of some daemon, you may need to be able to specify that the bind address is 0.0.0.0. The value can't be rejected due to not being an address. Also, you need to be able specify network as opposed to host addresses, and netmasks. You know, like 10.0.0.0 and so on.