Python3:正则表达式的使用

1.声明

当前的学习来源:python核心编程书籍
需要导入正则表达式模块:re

2.验证11位的手机号

# 使用正则匹配手机号 默认为11位
while True:
phone = input("请输入手机号:")
match = re.search("^(1[1539])\d{9}$", phone)

if match:
    print("是手机号!")
else:
    print("不是手机号!")

3.匹配指定的restful的url

1.使用match方式匹配的时候,没有返回None

# 使用正则的方式获取指定的url中的字段或者参数
url_str = "/user/del/1"
url_match = re.match("/(.*)/(.*)/(.*)", url_str)
print(url_match.groups())  # 获得的结果为:('user', 'del', '1')

4.匹配和替换字符

1.这里的匹配为:search,search用于匹配后分组,需要使用括号才能使用

2.替换的时候需要使用re.sub方式将当前匹配的字符替换掉,需要使用\1或者\2的占位符方式替换

words = "你好我的电话为:13955555555,以后有空常联系!记住电话为:13955555555"  # 直接匹配当前的电话
phone = re.search("(\d{11})", words, re.S)  # 将匹配的字符转换为
print(phone.groups())  # 使用search匹配正则的时候需要将查找的正则使用括号包起来
# 将电话中间的四位使用****号代替,结果为:139****5555

phone = "13911111111"
phone = re.sub("(\d{3})(\d{4})(\d{4})", '\\1****\\3', phone)  # \\1 就表示当前的括号里面的组1,\\3表示括号里面的组3
print(phone)

# 所以同理可以实现
words = re.sub("(\d{3})(\d{4})(\d{4})", '\\1****\\3', words)
print("被替换后的字符为:{0}".format(words))

5.正则匹配:bat bit but hat hit hut这些字符

# 正则匹配 :bat bit but hat hit hut
partten_words = ["bat", "bit", "but", "hat", "hit", "hut"]
# 通过观察发现当前的字符为 b或者h开头 中间为a i u 最后一位为:t
# 所以正则为:[b|h][a|i|u]t
pi_pei = re.match("[b|h][a|i|u]t", "b8t")  # 发现不匹配结果为None



def check_string():
    for word in partten_words:
        pi_pei = re.match("[b|h][a|i|u]t", word)
        if pi_pei is None:
            return False

    return True


print("当前集合的匹配结果为:{0}".format(check_string()))

6.正则匹配:单词空格单词

1.使用\s匹配一个空格,使用\w匹配一个单词,使用+匹配一个或者多个

# 用于匹配姓和名
name = "bob smith"  # 分析结果为:单词 空格 单词
# 当前的正则为:\w+\s\w+
result = re.match("\w+\s\w+", name)
print("当前的匹配结果:{0}".format(result is not None))

7.正则匹配:单词,空格单词

# 匹配一个逗号和空格分割的一个单词和一个字母
name = "Bon, M"
result = re.match("\w+,\s\w", name)
print("当前的匹配结果:{0}".format(result is not None))

8.匹配域名网址:www.XXX.com或者.edu

1.使用(a|b|c)方式匹配选择项

# 匹配web域名:www.baidu.com,或者www.yahoo.com这些义www.开头以.com结尾
# 使用的正则为 ^w{3}\.\w+\.com$
web_site = "www.baidu.com"
result = re.match("^w{3}\.\w+\.com$", web_site)
print("当前的匹配结果:{0}".format(result is not None))

web_site = "www.baidu.edu"
# 升级支持.edu和.net结尾的域名
result = re.match("^w{3}\.\w+\.(com|edu|net)$", web_site)
print("当前的匹配结果:{0}".format(result is not None))

9.正则匹配:整数字符,浮点字符

# 匹配所有的整数字符的集合
int_partten = "(-)?\d+"  # 用于匹配所有的正负整数
while True:
    input_int = input("请输入整数")
    result = re.match(int_partten, input_int)
    print("当前输入的数字正确!" if result is not None else "当前输入的数字不正确!")

# 匹配所有的浮点型的字符串
float_partten = "(-)?\d+(\.\d+)?"
while True:
    input_float = input("请输入浮点字符")
    result = re.match(float_partten, input_float)
    print("当前输入的数字正确!" if result is not None else "当前输入的数字不正确!")

10.正则匹配:邮箱:[email protected]或者cn

# 匹配邮箱 [email protected] 匹配这个邮箱
# 分析 \w+@\w+\.(com|cn)
email_partten = "\w+@\w+\.(com|cn)"
while True:
    input_email = input("请输入邮箱")
    result = re.match(email_partten, input_email)
    print("当前输入的邮箱正确!" if result is not None else "当前输入的邮箱不正确!")

11.正则匹配:<class ‘int’>中的int

print(type(1.0))  # 匹配<class 'int'>中的int
type_string = str(type(1.0))
# 使用的正则匹配 <class\s'(\w+)'>
type_partten = "<class\s'(\w+)'>"
# result = re.match(type_partten, type_string)
# print(result is not None)
result_group = re.findall(type_partten, type_string)
print(result_group[0])

print("============================")


def get_type_name(types_array=[]):
    for item in types_array:
        type_str = str(type(item))
        print(re.findall(type_partten, type_str)[0])


get_type_name([1, 1.1, 1.1j, "1.1", set(), dict(), {}, []])

12.总结

1.python中匹配正则需要导入re模块

2.使用search搜索带有括号()的正则表达式,通过groups迭代使用match匹配成功返回类型失败返回None

以上纯属个人见解,如有问题请联系本人!

发布了215 篇原创文章 · 获赞 39 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/103222036