python 正则表达式
Posted A running snail,little step ev
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 正则表达式相关的知识,希望对你有一定的参考价值。
基本语法
# 1. 一般字符: 匹配字符的本身 print(re.match("abc","abcdefg")) print("abc" in "abcdefg") # 2 【.】 --匹配一个除\\n 外所有的字符 # 需求: 三个字母,第一个是a print(re.match("^a..$","a35")) # 3 【\\】 --转义字符 print(re.match("a\\...","a.1#")) print(re.match("a[.]..","a.123")) print(re.match("a[.]..$","a.1#asdfasdf")) # 4 【】 --字符集中的任意一个字符 print(re.match("^a[bdc]e$","abe")) print(re.match("^a[bdc]e$","ade")) print(re.match("^a[bdc]e$","ace")) # 5 开头: ^ 结尾: $ --- 完全匹配 print(re.match("^a[bdc]f$","acf"))
<re.Match object; span=(0, 3), match=\'abc\'>
True
<re.Match object; span=(0, 3), match=\'a35\'>
<re.Match object; span=(0, 4), match=\'a.1#\'>
<re.Match object; span=(0, 4), match=\'a.12\'>
None
<re.Match object; span=(0, 3), match=\'abe\'>
<re.Match object; span=(0, 3), match=\'ade\'>
<re.Match object; span=(0, 3), match=\'ace\'>
<re.Match object; span=(0, 3), match=\'acf\'>
print(re.findall("\\^\\d{3}","^123dafd^888safd")) #2 【.】 --匹配一个除\\n 外所有的字符 print(re.findall("a.{3}","^123d adfd a3434 afd^888safd"))
[\'^123\', \'^888\']
[\'adfd\', \'a343\', \'afd^\']
#2 【.】 --匹配\\ 这个字符 print(re.findall("\\\\\\\\X","\\Xdfdfd \\Xadfd \\Xa3434 \\xafd^888safd")) print(re.findall("\\\\b","\\Xdfdfd \\Xadfd \\Xa3434 \\xafd^888safd")) #为了避免困扰,建议写正则时加R/r print(re.findall(r"\\\\b","\\Xdfdfd \\Xadfd \\Xa3434 \\xafd^888safd")) print(re.findall(r"\\\\","\\Xdfdfd \\Xadfd \\Xa3434 \\xafd^888safd"))
[\'\\\\X\', \'\\\\X\', \'\\\\X\']
[\'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\']
[]
[\'\\\\\', \'\\\\\', \'\\\\\']
print(re.findall(R"\\D\\s{2}\\w","a 4 fadfsdfasd adfasdf _f")) #匹配a-f开头 不区分大小写,后面三个小写字每句,后面以偶数结尾 print(re.findall(R"[a-zA-Z][a-z]{3}[02468]","asdfd2 4 fadfsdfasd6 adfasdf8 _f"))
[\'a 4\', \'f _\']
[\'sdfd2\', \'fasd6\', \'asdf8\']
print(re.findall(r"na[a-z]*e","my name is nae date nadwsdfe nadwsdfddde nadwsdfddfdfddsdde")) print(re.findall(r"na[a-z]+e","my name is nae date nadwsdfe nadwsdfddde nadwsdfddfdfddsdde")) print(re.findall(r"na[a-z]?e","my name is nae date nadwsdfe nadwsdfddde nadwsdfddfdfddsdde")) print(re.findall(r"na[a-z]{5}e","my name is nae date nadwsdfe nadwsdfddde nadwsdfddfdfddsdde")) print(re.match(r"(\\d{14}[0-9X])|(\\d{17}[0-9X])","411111111111111111")) print(re.findall(r"na[a-z]{5,10}e","my name is nae date nadwsdfe nadwsdfddde")) print(re.findall(r"na[a-z]{5,}e","my name is nae date nadwsdfe nadwsdfddde nadwsdfddfdfddsdde"))
[\'name\', \'nae\', \'nadwsdfe\', \'nadwsdfddde\', \'nadwsdfddfdfddsdde\']
[\'name\', \'nadwsdfe\', \'nadwsdfddde\', \'nadwsdfddfdfddsdde\']
[\'name\', \'nae\']
[\'nadwsdfe\']
<re.Match object; span=(0, 15), match=\'411111111111111\'>
[\'nadwsdfe\', \'nadwsdfddde\']
[\'nadwsdfe\', \'nadwsdfddde\', \'nadwsdfddfdfddsdde\']
import re
match_result = re.finditer(R"\\b([a-z])([a-z])[a-z]\\2\\1\\b","fdafd abcba ytgty asdsa")
match_list=[]
for i in match_result:
match_list.append(i.group(0))
print(match_list)
print(re.findall(R"\\b(?P<number01>[a-z])(?P<number02>[a-z])[a-z](?P=number02)(?P=number01)\\b","fdafd abcba ytgty asdsa"))
[\'abcba\', \'ytgty\', \'asdsa\']
[(\'a\', \'b\'), (\'y\', \'t\'), (\'a\', \'s\')]
print(re.findall(R"\\b(?<=name=)[a-z]+\\b","fdafd name=abcba name=ytgty name=asdsa"))
[\'abcba\', \'ytgty\', \'asdsa\']
以上是关于python 正则表达式的主要内容,如果未能解决你的问题,请参考以下文章