用于验证逗号分隔的 IPv4 和 cidr 的正则表达式
regex to validate comma seperated IPv4 and cidr
我有这个验证 IPV4 和 IPV6 IP 地址和 CIDR 的正则表达式,但我只想验证 ipv4 地址和 cidr
^(((((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-9]|1[0-9]|2[0-8]|3[0-2])){0,1}){0,1})|((((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])){0,1})))((\s*,\s*)(?=[^,])){0,1})+$
有人可以帮我解决这个问题吗?
删除不需要的:
^(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-9]|1[0-9]|2[0-8]|3[0-2])){0,1}){0,1}((\s*,\s*)(?=[^,])){0,1})+$
参见regex proof。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
( group and capture to (between 0 and 1
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
( group and capture to (3 times):
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2
times)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-4] any character of: '0' to '4'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
25 '25'
--------------------------------------------------------------------------------
[0-5] any character of: '0' to '5'
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
){3} end of (NOTE: because you are using
a quantifier on this capture, only the
LAST repetition of the captured
pattern will be stored in )
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2
times)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-4] any character of: '0' to '4'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
25 '25'
--------------------------------------------------------------------------------
[0-5] any character of: '0' to '5'
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
( group and capture to (between 0 and
1 times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-8] any character of: '0' to '8'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
3 '3'
--------------------------------------------------------------------------------
[0-2] any character of: '0' to '2'
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
){0,1} end of (NOTE: because you are using
a quantifier on this capture, only the
LAST repetition of the captured
pattern will be stored in )
--------------------------------------------------------------------------------
){0,1} end of (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in )
--------------------------------------------------------------------------------
( group and capture to (between 0 and 1
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
[^,] any character except: ','
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
){0,1} end of (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in )
--------------------------------------------------------------------------------
)+ end of (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in )
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
我有这个验证 IPV4 和 IPV6 IP 地址和 CIDR 的正则表达式,但我只想验证 ipv4 地址和 cidr
^(((((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-9]|1[0-9]|2[0-8]|3[0-2])){0,1}){0,1})|((((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])){0,1})))((\s*,\s*)(?=[^,])){0,1})+$
有人可以帮我解决这个问题吗?
删除不需要的:
^(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-9]|1[0-9]|2[0-8]|3[0-2])){0,1}){0,1}((\s*,\s*)(?=[^,])){0,1})+$
参见regex proof。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
( group and capture to (between 0 and 1
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
( group and capture to (3 times):
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2
times)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-4] any character of: '0' to '4'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
25 '25'
--------------------------------------------------------------------------------
[0-5] any character of: '0' to '5'
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
){3} end of (NOTE: because you are using
a quantifier on this capture, only the
LAST repetition of the captured
pattern will be stored in )
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2
times)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-4] any character of: '0' to '4'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
25 '25'
--------------------------------------------------------------------------------
[0-5] any character of: '0' to '5'
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
( group and capture to (between 0 and
1 times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
2 '2'
--------------------------------------------------------------------------------
[0-8] any character of: '0' to '8'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
3 '3'
--------------------------------------------------------------------------------
[0-2] any character of: '0' to '2'
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
){0,1} end of (NOTE: because you are using
a quantifier on this capture, only the
LAST repetition of the captured
pattern will be stored in )
--------------------------------------------------------------------------------
){0,1} end of (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in )
--------------------------------------------------------------------------------
( group and capture to (between 0 and 1
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
[^,] any character except: ','
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
){0,1} end of (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in )
--------------------------------------------------------------------------------
)+ end of (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in )
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string