Python 正则表达式验证IPv4地址(转)

来源:互联网 发布:zepto.js tap事件 编辑:程序博客网 时间:2024/06/05 11:59

转自:http://blog.csdn.net/guaguastd/article/details/40650197

1. Simple regex to check for an IP address

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

2. Accurate regex to check for an IP address, allowing leading zeros
^(?:(?: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]?)$

3. Accurate regex to check for an IP address, disallowing leading zeros
^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$

4. Simple regex to extract IP address from longer text
\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b

5. Accurate regex to extract IP addresses from longer text, allowing leading zeros
\b(?:(?: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]?)\b

6. Accurate regex to extract IP addresses from longer text, disallowing leading zeros
\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b

7. Simple regex that captures the four parts of the IP address
^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$

8. Accurate regex that captures the four parts of the IP address, allowing leading zeros
^(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]?)$

9. Accurate regex that captures the four parts of the IP address, disallowing leading zeros
^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$
0 0
原创粉丝点击