re模块常用的正则匹配

ip地址:

re.match(
            "^(d|[1-9]d|1d{2}|2[0-4]d|25[0-5]).(d|[1-9]d|1d{2}|2[0-4]d|25[0-5]).(d|[1-9]d|1d{2}|2[0-4]d|25[0-5]).(d|[1-9]d|1d{2}|2[0-4]d|25[0-5])$",
            ip_addr)

域名

# 以Https、Http开头
re.match(
            "^(http|https?://)([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?$", ip_addr
        )

端口号

([0-9]|[1-9]d{1,3}|[1-5]d{4}|6[0-4]d{4}|65[0-4]d{2}|655[0-2]d|6553[0-5])

带有端口号的ip地址和域名

domain = re.match(
            r'^(http|https?://)([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?(:([0-9]|[1-9]d{1,3}|[1-5]d{4}|6[0-4]d{4}|65[0-4]d{2}|655[0-2]d|6553[0-5]))?$',
            ip_addr)
ip = re.match(
            r'^(d|[1-9]d|1d{2}|2[0-4]d|25[0-5]).(d|[1-9]d|1d{2}|2[0-4]d|25[0-5]).(d|[1-9]d|1d{2}|2[0-4]d|25[0-5]).(d|[1-9]d|1d{2}|2[0-4]d|25[0-5])(:([0-9]|[1-9]d{1,3}|[1-5]d{4}|6[0-4]d{4}|65[0-4]d{2}|655[0-2]d|6553[0-5]))?$',
            ip_addr)

用户名(大小写字母、数字、下划线和中文)

限制长度

^[u4e00-u9fa5_a-zA-Z0-9]{4,10}$

不限制长度

^[u4e00-u9fa5_a-zA-Z0-9]+$

不能以下划线开头和结尾

^(?!_)(?!.*?_$)[a-zA-Z0-9_u4e00-u9fa5]+$

中文字符

[u4e00-u9fa5]

匹配双字节字符(包括汉字在内)

[^x00-xff]

python2中匹配中文字符

pattern = u"^[u4e00-u9fa5a-zA-Z0-9]+"
username = re.match(pattern, username)
原文地址:https://www.cnblogs.com/jiumo/p/11054513.html