python正则表达式百分数_Python正则表达式
正则表达式是处理字符串的强⼤的⼯具,它有⾃⼰特定的语法结构,有了它,实现字符串的检索、替换、匹配验证都不在话下。.
匹配任意字符,除了换⾏符,当re.DOTALL标记被指定时,则可以匹配包括换⾏符的任意字符
import re
a = 'xz123'
b = re.findall('x....',a)
print(b)
运⾏结果:
['xz123']
*
匹配0个或多个的表达式
import re
a = 'xyxy123'
b = re.findall('x*',a)
print(b)
运⾏结果:
['x', '', 'x', '', '', '', '', '']
匹配0个或1个由前⾯的正则表达式定义的⽚段,⾮贪婪⽅式
import re
a = 'xy123'
b = re.findall('x?',b)
print(b)
运⾏结果:
['x', '', '', '', '', '']
.*的使⽤举例
import re
secrect_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'
b = re.findall('xx.*xx',secrect_code)
print(b)
运⾏结果:
['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx']python正则表达式不包含
.*?的使⽤举例
secrect_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse' b = re.findall('xx.*?xx',secrect_code)
print(b)
运⾏结果:
['xxIxx', 'xxlovexx', 'xxyouxx']
(.*?)使⽤括号与不使⽤括号的差别
( )匹配括号内的表达式,也表⽰⼀个组
import re
secrect_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse' d = re.findall('xx(.*?)xx',secrect_code)
print(d)
for each in d:
print(each)
运⾏结果:
['I', 'love', 'you']
I
love
you
re.S的作⽤使.的作⽤包括了\n
import re
s = '''sdfxxhelloxxfsdf
xxworldxxasdf'''
d = re.findall('xx(.*?)xx',s,re.S)
print(d)
运⾏结果:
['hello', 'world']
$
匹配字符串的末尾
import re
s= 'detail.html?key=TISSUE INJURY'
f = re.findall('key=(.*?)',s)
print(f)
运⾏结果:
['']
import re
s= 'detail.html?key=TISSUE INJURY' f = re.findall('key=(.*?)$',s)
print(f)
运⾏结果:
['TISSUE INJURY']
\d
匹配任意数字,等价于 [0-9]
import re
s = 'asdfasf1234567fasd555fas' b = re.findall('(\d+)',s)
print(b)
运⾏结果:
['1234567', '555']
import re
s = 'asdfasf1234.567fasd55.5fas' b = re.findall('(\d+\.\d+)',s)
print(b)
运⾏结果:
['1234.567', '55.5']
\转义字符
import re
s = 'sda123sdaslda.s/'
b = re.search('\.',s)
print(b)
print(b.span())
up())
运⾏结果:
(13, 14)
.
a|b
匹配a或b
import re
s = 'fishdafishc'
b= re.search(r'fish(c|d)',s)
print(b)
运⾏结果:
{n}
精确匹配n个前⾯表达式**
import re
s ='i fishhhfish.a'
b = re.search(r'fish{3}',s)
up())
运⾏结果:
fishhh
s = 'i fishhhfishfishfish.a'
b = re.search(r'(fish){3}',s )
up())
运⾏结果:
fishfishfish
{n, m}
匹配 n 到 m 次由前⾯的正则表达式定义的⽚段,贪婪⽅式** import re
s = 'i fishhhfishfishfish.a'
b = re.search(r'(fish){2,5}',s)
up())
运⾏结果:
fishfishfish
sub的使⽤举例
import re
s = '123rrrrr123'
output = re.sub('123(.*?)123','123%d123'%789,s) print(output)
运⾏结果:
123789123
#把⼀串⽂本中的所有数字都去掉
content = '54aK54yr5oiR54ix5L2g'
content = re.sub('\d+', '', content)
print(content)
运⾏结果:
aKyroiRixLg
findall,search和match的区别
match
从⾸字母开始开始匹配,string如果包含pattern⼦串,则匹配成功,返回Match对象
search
若string中包含pattern⼦串,则返回Match对象,否则返回None,注意,如果string中存在多个pattern⼦串,只返回第⼀个findall
返回string中所有与pattern相匹配的全部字串,返回形式为数组
>>>import re
>>>s = '123.gifgdfkgj.jpg1673.gifgdfk66.jpg'
>>>m = re.match(r'(\d*)(\.gif|\.jpg)',s)
>>&up()
'123.gif'
>>&up(0)
'123.gif'
>>&up(1)
'123'
>>&up(2)
'.gif'
>>&ups()
('123', '.gif')
------------------------------------------------------------
>>>m = re.findall(r'(\d*)(\.gif|\.jpg)',s)
>>>m
[('123', '.gif'), ('', '.jpg'), ('1673', '.gif'), ('66', '.jpg')]
------------------------------------------------------------
>>>s = 'gdfkg1.jpg1673.gifgdfk66.jpg'
>>>m = re.search(r'.(\d*)(\.gif|\.jpg)',s)
>>>m
>>&up()
'g1.jpg'
>>&up(0)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。