正则表达式

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. 正则表达式

正则表达式(Regular Expression)描述了一种字符串匹配模式,主要用来检索、替换匹配某种模式的字符串。

2. 正则表达式语法

下面以Python代码来展示正则表达式的匹配。

  • .
    .可以匹配任意单个字符,除了换行符。例如.可匹配abc中的任意一个字符。
import re
print(re.findall(r'.', 'abc'))

# 代码执行结果
['a', 'b', 'c']
  • ^
    ^表示字符串的开始,例:^Th表示匹配以Th开头的字符串。
import re
print(re.findall(r'^Th', 'This is a demo. This is a demo.'))

# 代码执行结果
['Th']
  • $
    $表示字符串的结束,例:demo$表示匹配以demo结尾的字符串。
import re
print(re.findall(r'demo$', 'This is a demo. This is a demo'))

# 代码执行结果
['demo']
  • *
    *匹配>=0个在*号之前的字符。例:test*表示匹配以tes为起始值,其后为0个t或多个t的字符串。
import re
print(re.findall(r'test*', 't te tes test testt'))

# 代码执行结果
['tes', 'test', 'testt']
  • +
    +匹配>=1个在+号之前的字符。例:test+表示匹配以tes为起始值,其后为1个t或多个t的字符串。
import re
print(re.findall(r'test+', 't te tes test testt'))

# 代码执行结果
['test', 'testt']
  • ?
    ?之前的字符为可选字符。例:test?表示匹配以tes为起始值,其后为1个t或没有t的字符串。
import re
print(re.findall(r'test?', 't te tes test testt'))

# 代码执行结果
['tes', 'test', 'test']
  • \
    \为转义字符,用于匹配一些保留的字符[ ] ( ) { } . * + ? ^ $ \ |
import re
print(re.findall(r'test\?', 't te tes test? testt'))

# 代码执行结果
['test?']
  • |
    |为或运算符,匹配符号前或后的字符。例:te|st表示匹配test的字符串。
import re
print(re.findall(r'te|st', 't te tes test'))

# 代码执行结果
['te', 'te', 'te', 'st']
  • [ ]
    [ ]表示要匹配的字符种类,匹配方括号内的任意字符。例:[test]匹配括号中的任意一个字符。
import re
print(re.findall(r'[test]', 'This is a test'))

# 代码执行结果
['s', 's', 't', 'e', 's', 't']
  • [^ ]
    [^ ]表示不进行匹配的字符种类,匹配除了方括号里字符之外的任意字符。
import 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-2t的字符串。
import re
print(re.findall(r'test{1,2}', 'This is a test testt'))

# 代码执行结果
['test', 'testt']
  • (xyz)
    (xyz)表示匹配与()内容完全相同的字符串。例:(test){1,2}表示匹配1-2testtest是一个整体。
import re
print(re.findall(r'(test){1,2}', 'This is a test testt'))

# 代码执行结果
['test', 'test']
  • \w
    \w匹配所有字母数字以及下划线,即[a-zA-z0-9_]
import re
print(re.findall(r'\w', 'Is this a test?_'))

# 代码执行结果
['I', 's', 't', 'h', 'i', 's', 'a', 't', 'e', 's', 't']
  • \W
    \W匹配字母数字以及下划线之外的字符,即[^\w]
import re
print(re.findall(r'\W', 'Is this a test?'))

# 代码执行结果
[' ', ' ', ' ', '?']
  • \d
    \d匹配数字,即[0-9]
import re
print(re.findall(r'\d', 'test 123'))

# 代码执行结果
['1', '2', '3']
  • \D
    \D匹配数字之外的字符,即[^\d]
import re
print(re.findall(r'\D', 'test 123'))

# 代码执行结果
['t', 'e', 's', 't', ' ']
  • \s
    \s匹配所有空格字符,即[\t\n\f\r\p{Z}]
import re
print(re.findall(r'\s', 'test 123\n'))

# 代码执行结果
[' ', '\n']
  • \S
    \S匹配非空格字符,即[^\s]
import re
print(re.findall(r'\S', 'test 123\n'))

# 代码执行结果
['t', 'e', 's', 't', '1', '2', '3']
  • \n
    \n匹配一个换行符。
import re
print(re.findall(r'\n', 'test 123\n'))

# 代码执行结果
['\n']
  • \f
    \f匹配一个换页符。
import re
print(re.findall(r'\f', 'test 123\f'))

# 代码执行结果
['\x0c']
  • \r
    \r匹配一个回车符。
import re
print(re.findall(r'\r', 'test 123\r'))

# 代码执行结果
['\r']
  • \t
    \t匹配一个制表符。
import re
print(re.findall(r'\t', 'test 123\t'))

# 代码执行结果
['\t']
  • \v
    \v匹配一个垂直制表符。
import re
print(re.findall(r'\v', 'test 123\v'))

# 代码执行结果
['\x0b']
  • ?=
    ?=是前置约束,表示要匹配的是?=之前的内容,但同时要匹配?=之后的内容,前置约束需要使用()。例:Th(?=is)表示要匹配Th,要找的是This中的Th
import re
print(re.findall(r'Th(?=is)', 'There or This or The?'))

# 代码执行结果,匹配的是This中的Th
['Th']
  • ?!
    ?!也是前置约束,但与?=正好相反,也是要匹配?!之前的内容,但同时要不匹配?!之后的内容,前置约束需要使用()。例:Th(?!is)表示要匹配Th,要找的是非This中的Th
import re
print(re.findall(r'Th(?!is)', 'There or This or The?'))

# 代码执行结果,匹配的是There, The中的Th
['Th', 'Th']
  • ?<=
    ?<=是后置约束,表示要匹配的是(?<=)之后的内容,但同时要匹配(?<=)括号内的内容,后置约束需要使用()。例:(?<=H)e表示要匹配e,要找的是He中的e
import re
print(re.findall(r'(?<=H)e', 'The or He or She?'))

# 代码执行结果,匹配的是He中的e
['e']
  • ?<!
    ?<!是后置约束,表示要匹配的是(?<!)之后的内容,但同时要不匹配(?<!)括号内的内容,后置约束需要使用()。例:(?<!H)e表示要匹配e,要找的是非He中的e
import re
print(re.findall(r'(?<!H)e', 'The or He or She?'))

# 代码执行结果,匹配的是The, She中的e
['e', 'e']

参考资料

  1. https://juejin.im/entry/59a651116fb9a024844938b5
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值