【Python】re正则表达式

Posted on 2016-10-22 15:46  Sunday是只喵  阅读(336)  评论(0编辑  收藏  举报

简单举几个常用的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