[英]regex for multiple IPs comma separated with or without subnet
I have regex for IPv4 address : 我有用于IPv4地址的正则表达式:
^(([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])$
^(([[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] )$
and i have regex for IPv4 CIDR range : 我有正则表达式的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])(/([0-9]|[1-2][0-9]|3[0-2]))$
^(([[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] )(/([0-9] | [1-2] [0-9] | 3 [0-2]))$
the issue is how should i repeat it using comma separated 问题是我应该如何使用逗号分隔重复
pattern: 图案:
XXX.XXX.XXX.XXX, XXX.XXX.XXX.XXX/XX, XX.XX.XX.XX, XX.XX.XX.XX/X , XX.XX.XX.X test data-- XXX.XXX.XXX.XXX,XXX.XXX.XXX.XXX / XX,XX.XX.XX.XX,XX.XX.XX.XX / X,XX.XX.XX.X测试数据-
123.123.13.11, 1.0.0.0, 1.0.0.1/3, 1.0.0.0/20 123.123.13.11、1.0.0.0、1.0.0.1 / 3、1.0.0.0 / 20
am using http://regexr.com/ to build by regex, the regex which i build is below and not working-- 我正在使用http://regexr.com/通过正则表达式进行构建,我构建的正则表达式在下面并且无法正常工作-
/(((([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]))((([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])(\/([0-9]|[1-2][0-9]|3[0-2]))),?)/g
Is this what you're looking for? 这是您要找的东西吗?
/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/(3[0-1]|2[0-9]|1[0-9]|[4-9]))\,?\b){1,}/g
Edit: Breakdown 编辑:故障
Match an IP address: 匹配IP地址:
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
(\\/(30|2[0-9]|1[0-9]|[4-9]))?
/
followed by a number between 4 and 31. /
后跟4到31之间的数字。
\\,?
Comma. 逗号。 Optional.
可选的。
?
Space. 空间。 Optional.
可选的。
\\b
End of word. \\b
字尾。
){1,}
End of capturing group. ){1,}
捕获组结束。 All at least once. 全部至少一次。
To loop surround regex with ()*
ex (<regex>)*
if matching start and end then move terminators out of loop like ^(regex)*$
如果匹配开始和结束,则用
()*
ex (<regex>)*
循环环绕正则表达式,然后将终止符移出循环,例如^(regex)*$
To match ,
or end-of-line append ([,\\s]+|$)
exclude \\s
if you don't want whitespace, +
means match one or more. 要匹配
,
或最终的行追加([,\\s]+|$)
排除\\s
,如果你不想要的空白, +
意味着匹配一个或多个。
This should work for you to match the whole string. 这应该适合您匹配整个字符串。 Remove
*
at end for valid parts; 最后删除
*
以获取有效部分; surround with ^ $ to match full string. 用^ $包围以匹配完整字符串。
IPV4 = (([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])
Optional subnet = (\/([4-9]|[12][0-9]|3[0-2]))?
coma or end of line = (,|$)
Putting it together = (((([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])(\/([4-9]|[12][0-9]|3[0-2]))?)([,\s]+|$))*
Or, for minimal group matching ((?!\\\\/)
is negative look ahead for /
, not all regex engines support negative look ahead) 或者,最小的组匹配
((?!\\\\/)
是负的向前看的/
,并非所有的正则表达式引擎支持负前瞻)
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/(3[0-1]|2[0-9]|1[0-9]|[4-9]))?(?!\/)\b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.