1Regexp::Common::net(3)User Contributed Perl DocumentationRegexp::Common::net(3)
2
3
4
6 Regexp::Common::net -- provide regexes for IPv4, IPv6, and MAC
7 addresses.
8
10 use Regexp::Common qw /net/;
11
12 while (<>) {
13 /$RE{net}{IPv4}/ and print "Dotted decimal IP address";
14 /$RE{net}{IPv4}{hex}/ and print "Dotted hexadecimal IP address";
15 /$RE{net}{IPv4}{oct}{-sep => ':'}/ and
16 print "Colon separated octal IP address";
17 /$RE{net}{IPv4}{bin}/ and print "Dotted binary IP address";
18 /$RE{net}{MAC}/ and print "MAC address";
19 /$RE{net}{MAC}{oct}{-sep => " "}/ and
20 print "Space separated octal MAC address";
21 }
22
24 Please consult the manual of Regexp::Common for a general description
25 of the works of this interface.
26
27 Do not use this module directly, but load it via Regexp::Common.
28
29 This modules gives you regular expressions for various style IPv4,
30 IPv6, and MAC (or ethernet) addresses.
31
32 "$RE{net}{IPv4}"
33 Returns a pattern that matches a valid IP address in "dotted decimal".
34 Note that while 318.99.183.11 is not a valid IP address, it does match
35 "/$RE{net}{IPv4}/", but this is because 318.99.183.11 contains a valid
36 IP address, namely 18.99.183.11. To prevent the unwanted matching, one
37 needs to anchor the regexp: "/^$RE{net}{IPv4}$/".
38
39 For this pattern and the next four, under "-keep" (See Regexp::Common):
40
41 $1 captures the entire match
42
43 $2 captures the first component of the address
44
45 $3 captures the second component of the address
46
47 $4 captures the third component of the address
48
49 $5 captures the final component of the address
50
51 "$RE{net}{IPv4}{dec}{-sep}"
52 Returns a pattern that matches a valid IP address in "dotted decimal".
53 Leading 0s are allowed, as long as each component does not exceed 3
54 digits.
55
56 If "-sep=P" is specified the pattern P is used as the separator. By
57 default P is "qr/[.]/".
58
59 "$RE{net}{IPv4}{strict}{-sep}"
60 Returns a pattern that matches a valid IP address in "dotted decimal",
61 but disallow any leading 0s.
62
63 If "-sep=P" is specified the pattern P is used as the separator. By
64 default P is "qr/[.]/".
65
66 "$RE{net}{IPv4}{hex}{-sep}"
67 Returns a pattern that matches a valid IP address in "dotted
68 hexadecimal", with the letters "A" to "F" capitalized.
69
70 If "-sep=P" is specified the pattern P is used as the separator. By
71 default P is "qr/[.]/". "-sep=""" and "-sep=" "" are useful
72 alternatives.
73
74 "$RE{net}{IPv4}{oct}{-sep}"
75 Returns a pattern that matches a valid IP address in "dotted octal"
76
77 If "-sep=P" is specified the pattern P is used as the separator. By
78 default P is "qr/[.]/".
79
80 "$RE{net}{IPv4}{bin}{-sep}"
81 Returns a pattern that matches a valid IP address in "dotted binary"
82
83 If "-sep=P" is specified the pattern P is used as the separator. By
84 default P is "qr/[.]/".
85
86 "$RE{net}{MAC}"
87 Returns a pattern that matches a valid MAC or ethernet address as colon
88 separated hexadecimals.
89
90 For this pattern, and the next four, under "-keep" (See
91 Regexp::Common):
92
93 $1 captures the entire match
94
95 $2 captures the first component of the address
96
97 $3 captures the second component of the address
98
99 $4 captures the third component of the address
100
101 $5 captures the fourth component of the address
102
103 $6 captures the fifth component of the address
104
105 $7 captures the sixth and final component of the address
106
107 This pattern, and the next four, have a "subs" method as well, which
108 will transform a matching MAC address into so called canonical format.
109 Canonical format means that every component of the address will be
110 exactly two hexadecimals (with a leading zero if necessary), and the
111 components will be separated by a colon.
112
113 "$RE{net}{MAC}{dec}{-sep}"
114 Returns a pattern that matches a valid MAC address as colon separated
115 decimals.
116
117 If "-sep=P" is specified the pattern P is used as the separator. By
118 default P is "qr/:/".
119
120 "$RE{net}{MAC}{hex}{-sep}"
121 Returns a pattern that matches a valid MAC address as colon separated
122 hexadecimals, with the letters "a" to "f" in lower case.
123
124 If "-sep=P" is specified the pattern P is used as the separator. By
125 default P is "qr/:/".
126
127 "$RE{net}{MAC}{oct}{-sep}"
128 Returns a pattern that matches a valid MAC address as colon separated
129 octals.
130
131 If "-sep=P" is specified the pattern P is used as the separator. By
132 default P is "qr/:/".
133
134 "$RE{net}{MAC}{bin}{-sep}"
135 Returns a pattern that matches a valid MAC address as colon separated
136 binary numbers.
137
138 If "-sep=P" is specified the pattern P is used as the separator. By
139 default P is "qr/:/".
140
141 "$RE{net}{IPv6}{-sep => ':'}{-style => 'HeX'}"
142 Returns a pattern matching IPv6 numbers. An IPv6 address consists of
143 eight groups of four hexadecimal digits, separated by colons. In each
144 group, leading zeros may be omitted. Two or more consecutive groups
145 consisting of only zeros may be omitted (including any colons
146 separating them), resulting into two sets of groups, separated by a
147 double colon. (Each of the groups may be empty; "::" is a valid
148 address, equal to "0000:0000:0000:0000:0000:0000:0000:0000"). The hex
149 numbers may be in either case.
150
151 If the "-sep" option is used, its argument is a pattern that matches
152 the separator that separates groups. This defaults to ":". The "-style"
153 option is used to denote which case the hex numbers may be. The
154 default style, 'HeX' indicates both lower case letters 'a' to 'f' and
155 upper case letters 'A' to 'F' will be matched. The style 'HEX'
156 restricts matching to upper case letters, and 'hex' only matches lower
157 case letters.
158
159 If "{-keep}" is used, $1 to $9 will be set. $1 will be set to the
160 matched address, while $2 to $9 will be set to each matched group. If a
161 group is omitted because it contains all zeros, its matching variable
162 will be the empty string.
163
164 Example:
165
166 "2001:db8:85a3::8a2e:370:7334" =~ /$RE{net}{IPv6}{-keep}/;
167 print $2; # '2001'
168 print $4; # '85a3'
169 print $6; # Empty string
170 print $8; # '370'
171
172 Perl 5.10 (or later) is required for this pattern.
173
174 "$RE{net}{domain}"
175 Returns a pattern to match domains (and hosts) as defined in RFC 1035.
176 Under I{-keep} only the entire domain name is returned.
177
178 RFC 1035 says that a single space can be a domainname too. So, the
179 pattern returned by "$RE{net}{domain}" recognizes a single space as
180 well. This is not always what people want. If you want to recognize
181 domainnames, but not a space, you can do one of two things, either use
182
183 /(?! )$RE{net}{domain}/
184
185 or use the "{-nospace}" option (without an argument).
186
187 RFC 1035 does not allow host or domain names to start with a digits;
188 however, this restriction is relaxed in RFC 1101; this RFC allows host
189 and domain names to start with a digit, as long as the first part of a
190 domain does not look like an IP address. If the "{-rfc1101}" option is
191 given (as in "$RE {net} {domain} {-rfc1101}"), we will match using the
192 relaxed rules.
193
195 RFC 1035
196 Mockapetris, P.: DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION.
197 November 1987.
198
199 RFC 1101
200 Mockapetris, P.: DNS Encoding of Network Names and Other Types.
201 April 1987.
202
204 Regexp::Common for a general description of how to use this interface.
205
207 Damian Conway damian@conway.org.
208
210 This package is maintained by Abigail (regexp-common@abigail.be).
211
213 Bound to be plenty.
214
215 For a start, there are many common regexes missing. Send them in to
216 regexp-common@abigail.be.
217
219 This software is Copyright (c) 2001 - 2017, Damian Conway and Abigail.
220
221 This module is free software, and maybe used under any of the following
222 licenses:
223
224 1) The Perl Artistic License. See the file COPYRIGHT.AL.
225 2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2.
226 3) The BSD License. See the file COPYRIGHT.BSD.
227 4) The MIT License. See the file COPYRIGHT.MIT.
228
229
230
231perl v5.38.0 2023-07-21 Regexp::Common::net(3)