正则表达式 [] 使用

import re

str = r'[.com|.cn]'
str1 = 'www.baidu.com'
r1 = re.compile(str)
res = r1.findall(str1)
print(res) #这里的[]中只匹配一个字符

输出结果为 ['.', '.', 'c', 'o', 'm']


pat = "[a-zA-Z]+://[^\s]*[.com|.cn]"
string = '<a href="https://www.baidu.com">百度</a>'
rst = re.compile(pat).search(string)
print(rst)

输出结果为['
我想知道为什么第二个输出[.com|.cn]里面 为什么匹配的不是单个字符?


Zr_0118
浏览 1114回答 2
2回答

幕布斯0664548

匹配结果
打开App,查看更多内容
随时随地看视频慕课网APP