java正则表达式匹配IP地址和端口号作为捕获组 [英] java regex matching ip address and port number as captured groups
问题描述
可以请任何人告诉我这个正则表达式有什么问题吗?
could please anybody tell me what is wrong with this regexp ?
((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\:([0-9]{2,5})
匹配此:assfasfas> 192.168.1.1:8080192.168.222.43:8286
我需要192.168.1.1和8080被捕获组
for matching this: assfasfas>192.168.1.1:8080192.168.222.43:8286
I need 192.168.1.1 and 8080 to be captured groups
谢谢
推荐答案
除非你真的,真的还必须进行IP地址验证,我建议你简化正则表达式,因为这个野兽太复杂了,只能匹配IP部分和端口部分。我的建议是
Unless you really, really have to do IP adress validation, as well, I suggest you simplify the regular expression, because this beast is far too complex for only matching "IP part" and "port part". My suggestion would be
(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})
第1组和第2组将分别保留IP和端口。以上情况已经变得非常复杂了,恕我直言,即使这样简单就足够了:
Groups 1 and 2 will hold IP and port, respectively. And the above is already more complex that it needs to be, IMHO even something as simple as this would be enough:
(\d+\.\d+\.\d+\.\d+):(\d+)
请注意,双重反斜杠是Java字符串的要求,而不是正则表达式的要求,所以我把它们排除了。
Note that double backslashes are are requirement of Java strings, not of regex, so I left them out.
这篇关于java正则表达式匹配IP地址和端口号作为捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!