Pythonre正则表达式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pythonre正则表达式相关的知识,希望对你有一定的参考价值。

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

 


以上是关于Pythonre正则表达式的主要内容,如果未能解决你的问题,请参考以下文章

如何避免使用 Python re 库删除文本文件中正则表达式标志之间的文本块?

正则表达式整理

Python正则表达式多行匹配问题

正则表达式原始字符串及re

python的正则表达式

通过 Java 正则表达式提取 semver 版本字符串的片段