文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. 正则表达式
正则表达式(Regular Expression)描述了一种字符串匹配模式,主要用来检索、替换匹配某种模式的字符串。
2. 正则表达式语法
下面以Python代码来展示正则表达式的匹配。
..可以匹配任意单个字符,除了换行符。例如.可匹配abc中的任意一个字符。1
2
3
4
5import re
print(re.findall(r'.', 'abc'))
# 代码执行结果
['a', 'b', 'c']^^表示字符串的开始,例:^Th表示匹配以Th开头的字符串。1
2
3
4
5import re
print(re.findall(r'^Th', 'This is a demo. This is a demo.'))
# 代码执行结果
['Th']$$表示字符串的结束,例:demo$表示匹配以demo结尾的字符串。1
2
3
4
5import re
print(re.findall(r'demo$', 'This is a demo. This is a demo'))
# 代码执行结果
['demo']**匹配>=0个在*号之前的字符。例:test*表示匹配以tes为起始值,其后为0个t或多个t的字符串。1
2
3
4
5import re
print(re.findall(r'test*', 't te tes test testt'))
# 代码执行结果
['tes', 'test', 'testt']++匹配>=1个在+号之前的字符。例:test+表示匹配以tes为起始值,其后为1个t或多个t的字符串。1
2
3
4
5import re
print(re.findall(r'test+', 't te tes test testt'))
# 代码执行结果
['test', 'testt']??之前的字符为可选字符。例:test?表示匹配以tes为起始值,其后为1个t或没有t的字符串。1
2
3
4
5import re
print(re.findall(r'test?', 't te tes test testt'))
# 代码执行结果
['tes', 'test', 'test']\\为转义字符,用于匹配一些保留的字符[ ] ( ) { } . * + ? ^ $ \ |。1
2
3
4
5import re
print(re.findall(r'test\?', 't te tes test? testt'))
# 代码执行结果
['test?']||为或运算符,匹配符号前或后的字符。例:te|st表示匹配te或st的字符串。1
2
3
4
5import re
print(re.findall(r'te|st', 't te tes test'))
# 代码执行结果
['te', 'te', 'te', 'st'][ ][ ]表示要匹配的字符种类,匹配方括号内的任意字符。例:[test]匹配括号中的任意一个字符。1
2
3
4
5import re
print(re.findall(r'[test]', 'This is a test'))
# 代码执行结果
['s', 's', 't', 'e', 's', 't'][^ ][^ ]表示不进行匹配的字符种类,匹配除了方括号里字符之外的任意字符。1
2
3
4
5import re
print(re.findall(r'[^test]', 'This is a test'))
# 代码执行结果
['T', 'h', 'i', ' ', 'i', ' ', 'a', ' ']{m,n}{m,n}表示匹配(n-m+1)个大括号之前的字符。例:test{1,2}表示匹配以tes为起始值,其后为1-2个t的字符串。1
2
3
4
5import re
print(re.findall(r'test{1,2}', 'This is a test testt'))
# 代码执行结果
['test', 'testt'](xyz)(xyz)表示匹配与()内容完全相同的字符串。例:(test){1,2}表示匹配1-2个test,test是一个整体。1
2
3
4
5import re
print(re.findall(r'(test){1,2}', 'This is a test testt'))
# 代码执行结果
['test', 'test']\w\w匹配所有字母数字以及下划线,即[a-zA-z0-9_]。1
2
3
4
5import re
print(re.findall(r'\w', 'Is this a test?_'))
# 代码执行结果
['I', 's', 't', 'h', 'i', 's', 'a', 't', 'e', 's', 't']\W\W匹配字母数字以及下划线之外的字符,即[^\w]。1
2
3
4
5import re
print(re.findall(r'\W', 'Is this a test?'))
# 代码执行结果
[' ', ' ', ' ', '?']\d\d匹配数字,即[0-9]。1
2
3
4
5import re
print(re.findall(r'\d', 'test 123'))
# 代码执行结果
['1', '2', '3']\D\D匹配数字之外的字符,即[^\d]。1
2
3
4
5import re
print(re.findall(r'\D', 'test 123'))
# 代码执行结果
['t', 'e', 's', 't', ' ']\s\s匹配所有空格字符,即[\t\n\f\r\p{Z}]。1
2
3
4
5import re
print(re.findall(r'\s', 'test 123\n'))
# 代码执行结果
[' ', '\n']\S\S匹配非空格字符,即[^\s]。1
2
3
4
5import re
print(re.findall(r'\S', 'test 123\n'))
# 代码执行结果
['t', 'e', 's', 't', '1', '2', '3']\n\n匹配一个换行符。1
2
3
4
5import re
print(re.findall(r'\n', 'test 123\n'))
# 代码执行结果
['\n']\f\f匹配一个换页符。1
2
3
4
5import re
print(re.findall(r'\f', 'test 123\f'))
# 代码执行结果
['\x0c']\r\r匹配一个回车符。1
2
3
4
5import re
print(re.findall(r'\r', 'test 123\r'))
# 代码执行结果
['\r']\t\t匹配一个制表符。1
2
3
4
5import re
print(re.findall(r'\t', 'test 123\t'))
# 代码执行结果
['\t']\v\v匹配一个垂直制表符。1
2
3
4
5import re
print(re.findall(r'\v', 'test 123\v'))
# 代码执行结果
['\x0b']?=?=是前置约束,表示要匹配的是?=之前的内容,但同时要匹配?=之后的内容,前置约束需要使用()。例:Th(?=is)表示要匹配Th,要找的是This中的Th。1
2
3
4
5import re
print(re.findall(r'Th(?=is)', 'There or This or The?'))
# 代码执行结果,匹配的是This中的Th
['Th']?!?!也是前置约束,但与?=正好相反,也是要匹配?!之前的内容,但同时要不匹配?!之后的内容,前置约束需要使用()。例:Th(?!is)表示要匹配Th,要找的是非This中的Th。1
2
3
4
5import re
print(re.findall(r'Th(?!is)', 'There or This or The?'))
# 代码执行结果,匹配的是There, The中的Th
['Th', 'Th']?<=?<=是后置约束,表示要匹配的是(?<=)之后的内容,但同时要匹配(?<=)括号内的内容,后置约束需要使用()。例:(?<=H)e表示要匹配e,要找的是He中的e。1
2
3
4
5import re
print(re.findall(r'(?<=H)e', 'The or He or She?'))
# 代码执行结果,匹配的是He中的e
['e']?<!?<!是后置约束,表示要匹配的是(?<!)之后的内容,但同时要不匹配(?<!)括号内的内容,后置约束需要使用()。例:(?<!H)e表示要匹配e,要找的是非He中的e。1
2
3
4
5import re
print(re.findall(r'(?<!H)e', 'The or He or She?'))
# 代码执行结果,匹配的是The, She中的e
['e', 'e']