Android之IPv6服务器地址正则式匹配

1、带中括号(中括号内还需确认IPv6地址是否正确匹配,例如双冒号)

(??:http|https)/|//)(?:\S+(?::\S*)?@)?(??:\[{1})(??:[0-9a-z]*:{1,4}){1,7}[0-9a-z]{1,4})|localhost)(?:\]{1})(?::\d{2,5})?(?/|\?|#)[\s]*)?$

2、单纯匹配中括号内部IPv6地址

^\s*((([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|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[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|1\d\d|[1-9]?\d)(.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))?\s*$

格式:http://或者https:// + [IPv6地址] + :端口
http://[xxxx]:xxxxx
https://[xxxx]:xxxxx

匹配方法:(2)(3)组合使用
先使用(2)匹配外部格式,再取中括号内部IPv6地址,使用(3)匹配IPv6地址是否是否符合8组4位16进制

正则式在线测试工具:https://tool.oschina.net/regex

你可能感兴趣的:(android)