#分组 () print(re.findall("(ad)/(vv)","adddad/vvdddddddddd")) print(re.findall("(ad)/(d)","ad/ddad/vvdddddddddd")) print(re.findall("(a)(d)","adddad/vvdddddddddd")) #分组中间要有个字母和特殊符号隔开 print(re.findall("(ad)/d(da)","ad/ddad/vvdddddddddd")) print(re.search("(a)g(b)","adagbgdagbhagb").group()) print(re.findall("(a)g(b)","adagbgdagbh")) ret = re.search("(?P<id>d{2}):(?P<name>w{2})","25:a5") print(ret.group()) #25:a5 print(ret.group("id")) #25 print(ret.group("name")) #a5 print(re.search("a[bf]c","abcd").group()) #abc 字符串型 print(re.findall("a[bf]c","abcd")) #['abc'] 列表型 # | print(re.findall("a|b","afbgh")) #['a', 'b'] 需要匹配所有的a和b print(re.search("a|b","gaafbghagdbg").group()) #a 只需要匹配所有的a和b print(re.search("(ab)|f","gaabfbghagdbg").group()) #ab
print(re.search("d|gg","ds125ffgg").group()) #1
findall,match,search区别
print(re.findall("f","ffffffffgdsaadfasda12gtre5y7u")) #['ffffffff', 'f'] #匹配全部 print(re.search("f","gfffgdsaadfasda12gtre5y7u").group()) #f 匹配一个 print(re.match("a","gabd").group()) #a 只在开头匹配如果开头没有就会报错 “NoneType” | 相当于在findall中的"^a"
print(re.split("[ab]","abcg")) #['', '', 'cg'] print(re.split("[ab]","habg")) #['h', '', 'g'] print(re.sub("d","abc","aa5mm6nn7ff")) #第一个是最右边要替换的位置,中间是替换的内容 print("abcda".replace("a","kk",1)) #替换
print(re.sub("d","b","ab1cdb2cd")) rets = re.compile("d{2}") #编译 print(rets.search("av35ghd").group()) rett = re.finditer("d","a1n2nm5") for i in rett: print(next(rett).group())
print(re.findall("www.(baidu|oldbaidu).com","www.baidu.com")) #['baidu'] #会先匹配结果组里面的内容先返回 ,如果要得到全部结果需要取消权限 print(re.findall("www.(?:baidu|oldbaidu).com","www.baidu.com")) #['www.baidu.com']