正则findall包含匹配,求高手指点 - 爱问答

(爱问答)

正则findall包含匹配,求高手指点

re.findall(r"#(.*abc.*)#", target_string)

正则 re.findall  的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)
语法:

1findall(pattern, string, flags=0)


import re
Python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串
# print (help(re.findall))
# print (dir(re.findall))
findall查找全部r标识代表后面是正则的语句

123regular_v1 = re.findall(r"docs","https://docs.python.org/3/whatsnew/3.6.html")print (regular_v1)# ['docs']


符号^表示匹配以https开头的的字符串返回,

123regular_v2 = re.findall(r"^https","https://docs.python.org/3/whatsnew/3.6.html")print (regular_v2)# ['https']

 


用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串

123regular_v3 = re.findall(r"html$","https://docs.python.org/3/whatsnew/3.6.html")print (regular_v3)# ['html']


# [...]匹配括号中的其中一个字符

123regular_v4 = re.findall(r"[t,w]h","https://docs.python.org/3/whatsnew/3.6.html")print (regular_v4)# ['th', 'wh']


“d”是正则语法规则用来匹配0到9之间的数返回列表

123456regular_v5 = re.findall(r"d","https://docs.python.org/3/whatsnew/3.6.html")regular_v6 = re.findall(r"ddd","https://docs.python.org/3/whatsnew/3.6.html/1234")print (regular_v5)# ['3', '3', '6']print (regular_v6)# ['123']


小d表示取数字0-9,大D表示不要数字,也就是出了数字以外的内容返回

123regular_v7 = re.findall(r"D","https://docs.python.org/3/whatsnew/3.6.html")print (regular_v7)# ['h', 't', 't', 'p', 's', ':', '/', '/', 'd', 'o', 'c', 's', '.', 'p', 'y', 't', 'h', 'o', 'n', '.', 'o', 'r', 'g', '/', '/', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '/', '.', '.', 'h', 't', 'm', 'l']


“w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9

123regular_v8 = re.findall(r"w","https://docs.python.org/3/whatsnew/3.6.html")print (regular_v8)#['h', 't', 't', 'p', 's', 'd', 'o', 'c', 's', 'p', 'y', 't', 'h', 'o', 'n', 'o', 'r', 'g', '3', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '3', '6', 'h', 't', 'm', 'l']


“W”在正则里面代表匹配除了字母与数字以外的特殊符号

123regular_v9 = re.findall(r"W","https://docs.python.org/3/whatsnew/3.6.html")print (regular_v9)# [':', '/', '/', '.', '.', '/', '/', '/', '.', '.']


这个匹配以#开始,以#结束(不包含#)且包含abc的串

target_string="123#83das888abc9999wfsdf#ewrw"

匹配到的结果

['83das888abc9999wfsdf']

下一篇:电脑有DLL文件,却显示缺失

上一篇:开发者ID填写错误或者您的服务器不支持cURL的SSL模式访问

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?