首页 > 代码库 > 【Python】re正则表达式
【Python】re正则表达式
简单举几个常用的re正则表达式的例子:
1 import re
2 3 m = re.match("abc", "abcdef") 4 print(m.group()) # 输出:abc 5 m = re.match("[0-9][0-9]", "75ab6cdef") 6 print(m.group()) # 输出:75 7 m = re.match("[0-9]{0,10}", "75ab6cdef") 8 print(m.group()) # 输出:75 9 m = re.findall("[0-9]{1,10}","123124fsdfsf28") 10 print(m) # 输出:[‘123124‘, ‘28‘] 11 m = re.findall("[a-zA-Z]{1,10}","123124fsdfsf28fsdfsGG5DFSFDS3") 12 print(m) # 输出:[‘fsdfsf‘, ‘fsdfsGG‘, ‘DFSFDS‘] 13 m = re.findall(".*","123124fsdfsf28fsdfsGG5DFSFDS3") 14 print(m) # 输出:[‘123124fsdfsf28fsdfsGG5DFSFDS3‘, ‘‘] 15 m = re.findall(".","123fsf28fsG5DFSDS3") 16 print(m) # 输出:[‘1‘, ‘2‘, ‘3‘, ‘f‘, ‘s‘, ‘f‘, ‘2‘, ‘8‘, ‘f‘, ‘s‘, ‘G‘, ‘5‘, ‘D‘, ‘F‘, ‘S‘, ‘D‘, ‘S‘, ‘3‘] 17 m = re.findall(".+","123124fsdf~sf28.fsdf_sGG5DFSFDS3") 18 print(m) # 输出:[‘123124fsdf~sf28.fsdf_sGG5DFSFDS3‘] 19 m = re.search("\d+","aaa123124fsdf~sf28.fsdf_sGG5DFSFDS3") 20 print(m.group()) # 输出:1 21 m = re.sub("\d+","|","fsdf~sf28.fs df_sGG5DFSFDS3") 22 print(m) # 输出:fsdf~sf|.fs df_sGG|DFSFDS| 23 m = re.sub("\d+","|","fsdf~sf28.fs df_sGG5DFSFDS3",count=1) 24 print(m) # 输出:fsdf~sf|.fs df_sGG5DFSFDS3
【Python】re正则表达式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。