SYNOPSIS

    use Regexp::Common qw /net/;

    while (<>) {
        /$RE{net}{IPv4}/       and print "Dotted decimal IP address";
        /$RE{net}{IPv4}{hex}/  and print "Dotted hexadecimal IP address";
        /$RE{net}{IPv4}{oct}{-sep => ':'}/ and
                               print "Colon separated octal IP address";
        /$RE{net}{IPv4}{bin}/  and print "Dotted binary IP address";
        /$RE{net}{MAC}/        and print "MAC address";
        /$RE{net}{MAC}{oct}{-sep => " "}/ and
                               print "Space separated octal MAC address";
    }

DESCRIPTION

Please consult the manual of Regexp::Common for a general description of the works of this interface.

Do not use this module directly, but load it via Regexp::Common.

This modules gives you regular expressions for various style IPv4 and \s-1MAC\s0 (or ethernet) addresses. Returns a pattern that matches a valid \s-1IP\s0 address in \*(L"dotted decimal\*(R". Note that while 318.99.183.11 is not a valid \s-1IP\s0 address, it does match \*(C`/$RE{net}{IPv4}/\*(C', but this is because 318.99.183.11 contains a valid \s-1IP\s0 address, namely 18.99.183.11. To prevent the unwanted matching, one needs to anchor the regexp: \*(C`/^$RE{net}{IPv4}$/\*(C'.

For this pattern and the next four, under \*(C`-keep\*(C' (See Regexp::Common): captures the entire match captures the first component of the address captures the second component of the address captures the third component of the address captures the final component of the address Returns a pattern that matches a valid \s-1IP\s0 address in \*(L"dotted decimal\*(R"

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/[.]/\*(C'. Returns a pattern that matches a valid \s-1IP\s0 address in \*(L"dotted hexadecimal\*(R", with the letters \*(C`A\*(C' to \*(C`F\*(C' capitalized.

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/[.]/\*(C'. \*(C`-sep=""\*(C' and \*(C`-sep=" "\*(C' are useful alternatives. Returns a pattern that matches a valid \s-1IP\s0 address in \*(L"dotted octal\*(R"

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/[.]/\*(C'. Returns a pattern that matches a valid \s-1IP\s0 address in \*(L"dotted binary\*(R"

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/[.]/\*(C'. Returns a pattern that matches a valid \s-1MAC\s0 or ethernet address as colon separated hexadecimals.

For this pattern, and the next four, under \*(C`-keep\*(C' (See Regexp::Common): captures the entire match captures the first component of the address captures the second component of the address captures the third component of the address captures the fourth component of the address captures the fifth component of the address captures the sixth and final component of the address

This pattern, and the next four, have a \*(C`subs\*(C' method as well, which will transform a matching \s-1MAC\s0 address into so called canonical format. Canonical format means that every component of the address will be exactly two hexadecimals (with a leading zero if necessary), and the components will be separated by a colon.

The \*(C`subs\*(C' method will not work for binary \s-1MAC\s0 addresses if the Perl version predates 5.6.0. Returns a pattern that matches a valid \s-1MAC\s0 address as colon separated decimals.

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/:/\*(C'. Returns a pattern that matches a valid \s-1MAC\s0 address as colon separated hexadecimals, with the letters \*(C`a\*(C' to \*(C`f\*(C' in lower case.

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/:/\*(C'. Returns a pattern that matches a valid \s-1MAC\s0 address as colon separated octals.

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/:/\*(C'. Returns a pattern that matches a valid \s-1MAC\s0 address as colon separated binary numbers.

If \*(C`-sep=P\*(C' is specified the pattern P is used as the separator. By default P is \*(C`qr/:/\*(C'. Returns a pattern matching IPv6 numbers. An IPv6 address consists of eigth groups of four hexadecimal digits, separated by colons. In each group, leading zeros may be omitted. Two or more consecutive groups consisting of only zeros may be omitted (including any colons separating them), resulting into two sets of groups, separated by a double colon. (Each of the groups may be empty; \*(C`::\*(C' is a valid address, equal to \*(C`0000:0000:0000:0000:0000:0000:0000:0000\*(C'). The hex numbers may be in either case.

If the \*(C`-sep\*(C' option is used, its argument is a pattern that matches the separator that separates groups. This defaults to \*(C`:\*(C'. The \*(C`-style\*(C' option is used to denote which case the hex numbers may be. The default style, 'HeX' indicates both lower case letters 'a' to 'f' and upper case letters 'A' to 'F' will be matched. The style 'HEX' restricts matching to upper case letters, and 'hex' only matches lower case letters.

If \*(C`{-keep}\*(C' is used, $1 to $9 will be set. $1 will be set to the matched address, while $2 to $9 will be set to each matched group. If a group is omitted because it contains all zeros, its matching variable will be the empty string.

Example:

"2001:db8:85a3::8a2e:370:7334" =~ /$RE{net}{IPv6}{-keep}/; print $2; # '2001' print $4; # '85a3' print $6; # Empty string print $8; # '370'

Perl 5.10 (or later) is required for this pattern. Returns a pattern to match domains (and hosts) as defined in \s-1RFC\s0 1035. Under I{-keep} only the entire domain name is returned.

\s-1RFC\s0 1035 says that a single space can be a domainname too. So, the pattern returned by $RE{net}{domain} recognizes a single space as well. This is not always what people want. If you want to recognize domainnames, but not a space, you can do one of two things, either use

/(?! )$RE{net}{domain}/

or use the \*(C`{-nospace}\*(C' option (without an argument).

\s-1RFC\s0 1035 does not allow host or domain names to start with a digits; however, this restriction is relaxed in \s-1RFC\s0 1101; this \s-1RFC\s0 allows host and domain names to start with a digit, as long as the first part of a domain does not look like an \s-1IP\s0 address. If the \*(C`{-rfc1101}\*(C' option is given (as in \*(C`$RE {net} {domain} {-rfc1101}\*(C'), we will match using the relaxed rules.

REFERENCES

\s-1RFC\s0 1035

Mockapetris, P.: \s-1DOMAIN\s0 \s-1NAMES\s0 - \s-1IMPLEMENTATION\s0 \s-1AND\s0 \s-1SPECIFICATION\s0. November 1987.

\s-1RFC\s0 1101

Mockapetris, P.: \s-1DNS\s0 Encoding of Network Names and Other Types. April 1987.

RELATED TO Regexp::Common::net…

Regexp::Common for a general description of how to use this interface.

AUTHOR

Damian Conway [email protected].

MAINTAINANCE

This package is maintained by Abigail ([email protected]).

BUGS AND IRRITATIONS

Bound to be plenty.

For a start, there are many common regexes missing. Send them in to [email protected].

LICENSE and COPYRIGHT

This software is Copyright (c) 2001 - 2013, Damian Conway and Abigail.

This module is free software, and maybe used under any of the following licenses:

1) The Perl Artistic License. See the file COPYRIGHT.AL. 2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2. 3) The BSD Licence. See the file COPYRIGHT.BSD. 4) The MIT Licence. See the file COPYRIGHT.MIT.