IP address regex python

2019-07-18 23:45发布

I am having an issue with Regular expression, I need the most efficient regex that match IP address and in range of 255 only.

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

2条回答
Bombasti
2楼-- · 2019-07-19 00:30

Use this Regex. It will match and check the IP range within 255.

\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

查看更多
smile是对你的礼貌
3楼-- · 2019-07-19 00:32

This should do it:

^(?:(?: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]?)$
查看更多
登录 后发表回答