python(re模块)

正则就是模糊匹配

元字符(. ^ $ * + ? { } [ ] | ( ) )

. (通配符,出了 其他都可以匹配出来),一个点只代表一个符号

import re
s=re.findall('alex',"qweralexxcvbn")
print(s)

l=re.findall('a..x',"qweralexxcvbn")
print(l)

^ 表示以什么开头

import re
s=re.findall('^a..x',"qweralexxcvb")
print(s)

l=re.findall('^q..r',"qweralexxcvbn")
print(l)

$表示以什么结尾

import re
s=re.findall('a..x$',"qweralexxcvb")
print(s)

l=re.findall('a..x$',"qwerxcvbnalex")
print(l)

重复符号

*紧挨着的字符字符,表示重复很0到无穷次

import re
s=re.findall('alex*',"qweraaaaaaalecvb")
print(s)
import re
s=re.findall('alex*',"qweraaaaaaalexxxxxcvb")
print(s)

+表示重复很1到无穷次

import re
s=re.findall('alex+',"qweraaaaaaalexxxxxcvb")
print(s)

l=re.findall('alex+',"qwerxcvbnale")
print(l)

?表示重复0次或者1次

import re
s=re.findall('alex?',"qweraaaaaaalexxcvb")
print(s)

l=re.findall('alex?',"qwerxcvbnale")
print(l)

{ }重复次数自己定义

{0,}等价*

{1,}等价+

{0,1}等价于?

# -*- coding: utf-8 -*-
import re
#重复5次
s=re.findall('alex{5}',"qweraaaaaaalexxxxxxxcvb")
print(s)
#重复1到6次
l=re.findall('alex{1,6}',"qwerxcvbnalexxxxxx")
print(l)

以上匹配都是贪婪匹配,加上?就是惰性匹配

# -*- coding: utf-8 -*-
import re

s=re.findall('alex*?',"qweraaaaaaalexxxxxxxcvb")
print(s)

[  ]  中括号,字符集,起一个或的作用,中括号里没有特殊字符

# -*- coding: utf-8 -*-
import re

s=re.findall('a[b,d]c',"qwerabcadclexa,ccvb")
print(s)

特殊情况

1,范围

# -*- coding: utf-8 -*-
import re

s=re.findall('a[a-z]',"qwerabcadclexa,ccvb")
print(s)
# -*- coding: utf-8 -*-
import re
#[a-z]字符集重复
s=re.findall('a[a-z]*',"abc")
print(s)
# -*- coding: utf-8 -*-
import re
#[a-z]字符集重复
s=re.findall('a[1-9]*',"a12cd")
print(s)

^在中括号里表示非

# -*- coding: utf-8 -*-
import re
#不属于[a-z]全部匹配
s=re.findall('a[^a-z]*',"aQWERT12cdabcd")
print(s)

小应用

# -*- coding: utf-8 -*-
import re
#( )表示括号(转义),*表示多个字符
s=re.findall('([^()]*)',"4+(3-(1+1))")
print(s)

d匹配所有数字

# -*- coding: utf-8 -*-
import re

s=re.findall('d+',"14+(13-(11+1))")
print(s)

D匹配所有非数字部分

# -*- coding: utf-8 -*-
import re

s=re.findall('D+',"14+(13-(11+1))")
print(s)

s匹配任意空白字符

# -*- coding: utf-8 -*-
import re

s=re.findall('s+',"14+(13-(11+1)) abcd")
print(s)

S匹配任意非空白字符

# -*- coding: utf-8 -*-
import re

s=re.findall('S+',"14+(13-(11+1)) abcd")
print(s)

w匹配任意数字与字符

# -*- coding: utf-8 -*-
import re

s=re.findall('w+',"14+(13-(11+1)) abcd")
print(s)

W匹配任意非数字与字符

# -*- coding: utf-8 -*-
import re

s=re.findall('W+',"14+(13-(11+1)) abcd")
print(s)

匹配特殊字符边界空格,#,&等

小应用

# -*- coding: utf-8 -*-
import re
#没有特指
l=re.findall('www.baidu',"www1baidu")
print(l)

s=re.findall('www.baidu',"www.baidu")
print(s)

转义字符

r 表示原生字符串,正则表达式里使用""作为转义字符,这就可能造成反斜杠困扰。假如你需要匹配文本中的字符"",那么使用编程语言表示的正则表达式里将需要4个反斜杠"\":前两个和后两个分别用于在编程语言里转义成反斜杠,转换成两个反斜杠后再在正则表达式里转义成一个反斜杠。

# -*- coding: utf-8 -*-
import re
#拿不出来
s=re.findall('i',"hello i am bob")
print(s)

l=re.findall('i\b',"hello i am bob")
print(l)

r=re.findall(r'i',"hello i am bob")
print(r)
# -*- coding: utf-8 -*-
import re

s=re.findall('b\\l',"ablcdef")
print(s)

l=re.findall(r'b\l',"ablcdef")
print(l)

a|b    表示a或b

# -*- coding: utf-8 -*-
import re

s=re.findall('ab|h',"abcdef")
print(s)

l=re.findall('ab|h',"abcdhef")
print(l)
re.search()与re.findall()区别
# -*- coding: utf-8 -*-
import re
#返回一个列表
s=re.findall('ab.',"abcdeabf")
print(s)
#返回一个对象
l=re.search('ab.',"abcdheabf")
print(l)
re.search()匹配成功取出来
# -*- coding: utf-8 -*-
import re

l=re.search('ab.',"abcdheabf").group()
print(l)

元字符分组

# -*- coding: utf-8 -*-
import re

s=re.search("(?P<name>[a-z]+)(?P<age>d+)","bob23alex22").group("name")
print(s)

l=re.search("(?P<name>[a-z]+)(?P<age>d+)","bob23alex22").group("age")
print(l)
re.match()用法
re.match()用法同re.search()一样,但只在字符串开始处进行匹配
# -*- coding: utf-8 -*-
import re

l=re.match('bo',"bob23alex22").group()
print(l)

re.split()用法

# -*- coding: utf-8 -*-
import re
#按空格分
l=re.split(' ','hello abc|def')
print(l)

#按空格或分
m=re.split('[ |]','hello abc|def')
print(m)

#先按a分割,得空和bcd,结果再按b分割
n=re.split('[ab]','abcd')
print(n)

re.sub()用法(替换,3个参数)

# -*- coding: utf-8 -*-
import re
#数字替换为A
l=re.sub('d','A','a1b2c3d4')
print(l)
#数字替换为A,且仅从前往后替换2次
m=re.sub('d','A','a1b2c3d4',2)
print(m)

#全部替换并且统计次数
n=re.subn('d','A','a1b2c3d4')
print(n)
re.compile()用法
# -*- coding: utf-8 -*-
import re

l=re.compile('d+')
n=l.findall('qwe123asd456zxc')
print(n)
re.finditer()用法,将结果保存为迭代器,节省空间
# -*- coding: utf-8 -*-
import re

n=re.finditer('d','a1b2c3d4e5f6')
print(next(n).group())
print(next(n).group())
print(next(n).group())

补充

# -*- coding: utf-8 -*-
import re
#不会显示完整结果
n=re.findall('www.(baidu|163).com','www.baidu.com')
print(n)
#去优先级
n=re.findall('www.(?:baidu|163).com','www.baidu.com')
print(n)
原文地址:https://www.cnblogs.com/2018-1025/p/10223684.html