更新时间:2025-07-02 21:07点击:126
一、基本匹配规则
字面值字符:例如字母、数字、空格等,可以直接匹配它们自身。
特殊字符:例如点号 .、星号 *、加号 +、问号 ? 等,它们具有特殊的含义和功能。
字符类:用方括号 [ ] 包围的字符集合,用于匹配方括号内的任意一个字符。
元字符:例如 \d、\w、\s 等,用于匹配特定类型的字符,如数字、字母、空白字符等。
量词:例如 {n}、{n,}、{n,m} 等,用于指定匹配的次数或范围。
边界符号:例如 ^、$、\b、\B 等,用于匹配字符串的开头、结尾或单词边界位置。
python中re库的函数
import re
string="abcdefg acbdgef abcdgfe cadbgfe"
#带括号与不带括号的区别
#不带括号
regex=re.compile("((\w+)\s+\w+)")
print(regex.findall(string))
#输出:[('abcdefg acbdgef', 'abcdefg'), ('abcdgfe cadbgfe', 'abcdgfe')]
regex1=re.compile("(\w+)\s+\w+")
print(regex1.findall(string))
#输出:['abcdefg', 'abcdgfe']
regex2=re.compile("\w+\s+\w+")
print(regex2.findall(string))
#输出:['abcdefg acbdgef', 'abcdgfe cadbgfe']
1.首先导入模块
import re
2.匹配多种可能 使用 []
#'run' or 'ran' res = re.search(r'r[au]n','dog runs to cat') print(res) >>> <re.Match object; span=(4, 7), match='run'>3.匹配数字 \d and \D
# \d : decimal digit 数字的 res = re.search(r'r\dn','run r9n') print(res) >>> <re.Match object; span=(4, 7), match='r9n'> # \D : any non-decimal digit 任何不是数字的 res = re.search(r'r\Dn','run r9n') print(res) >>> <re.Match object; span=(0, 3), match='run'>4.匹配空白 \s and \S
# \s : any white space [\t \n \r \f \v] res = re.search(r'r\sn','r\nn r9n') print(res) >>> <re.Match object; span=(0, 3), match='r\nn'> # \S : 和\s相反,any non-white space res = re.search(r'r\Sn','r\nn r9n') print(res) >>> <re.Match object; span=(4, 7), match='r9n'>5.匹配所有的字母和数字以及"_" \w and \W
# \w : [a-zA-Z0-9_]# \W : opposite to \w 即与\w相反6.匹配空白字符 \b and \B
# \b : (only at the start or end of the word)# \B : ( but not at the start or end of the word)7.匹配特殊字符 任意字符 \ and .
# \\ : 匹配 \# . : 匹配 anything (except \n)8.匹配句尾句首 $ and ^
# ^ : 匹配line beginning# $ : 匹配line ending9. 是否匹配 ?
# ? : may or may nt occur10. 多行匹配 re.M
# 匹配代码后面加上re.M string = """ 123. dog runs to cat. You run to dog. """ res = re.search(r'^You',string) print(res) >>> None res = re.search(r'^You',string,re.M) print(res) >>> <re.Match object; span=(10, 13), match='run'>11. 匹配零次或多次 *
# * : occur 0 or more times res = re.search(r'ab*','a') print(res) >>> <re.Match object; span=(0, 1), match='a'> res = re.search(r'ab*','abbbbbbbbbb') print(res) >>> <re.Match object; span=(0, 11), match='abbbbbbbbbb'>12. 匹配一次或多次 +
# + :occur 1 or more times res = re.search(r'ab+','a') print(res) >>> None res = re.search(r'ab+','abbbbbbbbbb') print(res) >>> <re.Match object; span=(0, 11), match='abbbbbbbbbb'>13. 可选次数匹配 {n, m}
# {n, m} : occur n to m times res = re.search(r'ab{1,10}','a') print(res) >>> None res = re.search(r'ab{1,10}','abbbbbbbbbb') print(res) >>> <re.Match object; span=(0, 11), match='abbbbbbbbbb'>14. 匹配后group组输出
# group res = re.search(r'ID: (\d+), Name: (.+)','ID: 123456789, Name: a/b*c;d') print(res.group()) print(res.group(1)) print(res.group(2)) >>> ID: 123456789, Name: a/b*c;d >>> 123456789 >>> a/b*c;d # 给group组命名 ?P<name> res = re.search(r'ID: (?P<id>\d+), Name: (?P<name>.+)','ID: 123456789, Name: a/b*c;d') print(res.group('id')) print(res.group('name')) >>> 123456789 >>> a/b*c;d15.寻找所有匹配 findall
# re.findall() res = re.findall(r'r[ua]n','run ran ren') print(res) >>> ['run', 'ran'] # 另一种写法 res = re.findall(r'(run|ran)','run ran ren') print(res) >>> ['run', 'ran']16.替换匹配内容 sub
# re.sub( ,replace, ) res = re.sub(r'runs','catches','dog runs to cat') print(res) >>> dog catches to cat# re.split() res = re.split(r'[,;\.\\]', 'a,b;c.d\e') print(res) >>> ['a', 'b', 'c', 'd', 'e']17. 分裂内容 split
18. 包装正则表达式 compile
# re. compile() compile_re = re.compile(r'r[ua]n') res = compile_re.findall('run ran ren') print(res) >>> ['run', 'ran']4