Module: IpRegexes

Included in:
TopinambourRegex
Defined in:
lib/terminal_regex.rb

Constant Summary collapse

S4_DEF =

S4: IPv4 segment (number between 0 and 255) with lookahead at the end so that we don’t match “25” in the string “256”. The lookahead could go to the last segment of IPv4 only but this construct allows nicer unittesting. */

"(?(DEFINE)(?<S4>(?x: (?: [0-9] | [1-9][0-9] | 1[0-9]{2} | 2[0-4][0-9] | 25[0-5] ) (?! [0-9] ) )))"
IPV4_DEF =

IPV4: Decimal IPv4, e.g. “1.2.3.4”, with lookahead (implemented in S4) at the end so that we don’t match “192.168.1.123” in the string “192.168.1.1234”. */

"#{S4_DEF}(?(DEFINE)(?<IPV4>(?x: (?: (?&S4) \\. ){3} (?&S4) )))"
S6_DEF =

IPv6, including embedded IPv4, e.g. “::1”, “dead:beef::1.2.3.4”. Lookahead for the next char not being a dot or digit, so it doesn’t get stuck matching “dead:beef::1” in “dead:beef::1.2.3.4”. This is not required since the surrounding brackets would trigger backtracking, but it allows nicer unittesting. TODO: more strict check (right number of colons, etc.) TODO: add zone_id: RFC 4007 section 11, RFC 6874 */ S6: IPv6 segment, S6C: IPv6 segment followed by a comma, CS6: comma followed by an IPv6 segment */

"(?(DEFINE)(?<S6>[[:xdigit:]]{1,4})(?<CS6>:(?&S6))(?<S6C>(?&S6):))"
IPV6_FULL =
No

shorthand */

"(?x: (?&S6C){7} (?&S6) )"
IPV6_LEFT =
Begins with

*/

"(?x: : (?&CS6){1,7} )"
IPV6_MID =

somewhere in the middle - use negative lookahead to make sure there aren’t too many colons in total */

"(?x: (?! (?: [[:xdigit:]]*: ){8} ) (?&S6C){1,6} (?&CS6){1,6} )"
IPV6_RIGHT =
Ends with

*/

"(?x: (?&S6C){1,7} : )"
IPV6_NULL =

Is “::” and nothing more */

"(?x: :: )"
IPV6V4_FULL =

The same ones for IPv4-embedded notation, without the actual IPv4 part */

"(?x: (?&S6C){6} )"
IPV6V4_LEFT =

includes “::<ipv4>” */

"(?x: :: (?&S6C){0,5} )"
IPV6V4_MID =
"(?x: (?! (?: [[:xdigit:]]*: ){7} ) (?&S6C){1,4} (?&CS6){1,4} ) :"
IPV6V4_RIGHT =
"(?x: (?&S6C){1,5} : )"
IP_DEF =

IPV6: An IPv6 address (possibly with an embedded IPv4). This macro defines both IPV4 and IPV6, since the latter one requires the former. */

"#{IPV4_DEF}#{S6_DEF}(?(DEFINE)(?<IPV6>(?x: (?: #{IPV6_NULL} | #{IPV6_LEFT} | #{IPV6_MID} | #{IPV6_RIGHT} \
| #{IPV6_FULL} | (?: #{IPV6V4_FULL} | #{IPV6V4_LEFT} | #{IPV6V4_MID} | #{IPV6V4_RIGHT} \
) (?&IPV4) ) (?! [.:[:xdigit:]] ) )))"