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"; }
Do not use this module directly, but load it via Regexp::Common.
This modules gives you regular expressions for various style IPv4, IPv6, and MAC (or ethernet) addresses.
For this pattern and the next four, under "-keep" (See Regexp::Common):
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/[.]/".
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/[.]/".
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/[.]/". "-sep=""" and "-sep=" "" are useful alternatives.
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/[.]/".
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/[.]/".
For this pattern, and the next four, under "-keep" (See Regexp::Common):
This pattern, and the next four, have a "subs" method as well, which will transform a matching MAC 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.
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/:/".
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/:/".
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/:/".
If "-sep=P" is specified the pattern P is used as the separator. By default P is "qr/:/".
If the "-sep" option is used, its argument is a pattern that matches the separator that separates groups. This defaults to ":". The "-style" 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 "{-keep}" 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.
RFC 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 "{-nospace}" option (without an argument).
RFC 1035 does not allow host or domain names to start with a digits; however, this restriction is relaxed in RFC 1101; this RFC allows host and domain names to start with a digit, as long as the first part of a domain does not look like an IP address. If the "{-rfc1101}" option is given (as in "$RE {net} {domain} {-rfc1101}"), we will match using the relaxed rules.
For a start, there are many common regexes missing. Send them in to regexp-common@abigail.be.
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 License. See the file COPYRIGHT.BSD. 4) The MIT License. See the file COPYRIGHT.MIT.