Using \\d{1,3} when creating regex to find IPs

2019-08-22 03:28发布

问题:

Why would one use {1,3} in \d{1,3} when catching an IP with grep? For example:

grep -Po 'inet addr:\K(?!127\.)\d{1,3}.\d{1,3}\.\d{1,3}\.\d{1,3}'

\K removes inet addr:, and (?!127\.), AFAIU, removes any address that starts with 127 (the loopback in that case), but what are the {1,3} after \d?

Clearly, we don't only want IP calsses that starts in 1 and end with 2 or 3 so the purpose there is unclear to me.

Note: inet addr: is part of the ifconfig Linux utility.

回答1:

While writing the question I figured out the purpose: It means that in each class of the 4 classes, we will have not more than 3 digits.

Indeed in IPv4 (I don't know about IPv6) we have only 3 digits in each class.



回答2:

You have answered your question yourself however note that for general IPv4 the regex that should be used is the following:

'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b'
                         ^^^^^^^^^^^^^^^^

that you could adapt to remove the localhost one.

In your case, the grep will also fetch chains of digits that are not proper IPs (e.g. integers > 255)



标签: regex ip