简体   繁体   English

IP地址正则表达式python

[英]IP address regex python

I am having an issue with Regular expression, I need the most efficient regex that match IP address and in range of 255 only. 我在使用正则表达式时遇到问题,我需要与IP地址匹配且仅在255范围内的最高效的正则表达式。

I tried this one " ip_pattern = '\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}' " , but it does match even numbers over 255, such as 321.222.11.4 我尝试了这个“ ip_pattern = '\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}' “,但它甚至匹配超过255的数字,例如321.222.11.4

应该这样做:

^(?:(?: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]?)$

Use this Regex. 使用此正则表达式。 It will match and check the IP range within 255. 它将匹配并检查255之内的IP范围。

\\b(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]).(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]).(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]).(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\\b \\ B(:???25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9])。(?: 25 [O- 5] | 2 [0-4] [0-9] |?[0-1] [0-9] [0-9])(?: 25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9])(?: 25 [0-5] |?2 [0-4] [0-9] | [O- 1]?[0-9] [0-9])\\ b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM