正则表达式

https://c.runoob.com/front-end/854

-- coding:utf-8 --

import re

search_cod = 'feifjeifxxixxefijeifjeifejfxxlovexxfefi4455xxyouxxfefefefedss'

a = 'xy34455' #点就是点位符
b = re.findall('x..', a)
print(b)

c = 'xyxfefefefx'
d = re.findall('x*', c)#没有找到的用星号代替
print(d)

? 也是占位

e = 'tfefeifet'
f = re.findall('t?',e)
print(f)

ai1 = re.findall('xx.*xx', search_cod)
print(ai1)

ai2 = re.findall('xx.*?xx', search_cod)
print(ai2)

ai3 = re.findall('xx(.*?)xx', search_cod)
print(ai3)

非贪心算法 .*? 少量多餐

贪心算法 .* 尽可能多的匹配

(.*?) 取括号里的

h = '''sdfxxhello
xxfsdfxxworldxxasdffef'''
i = re.findall('xx(.*?)xx', h, re.S)#re.s 忽略换行匹配
print(i)

search 分组

s3 = 'asdfxxIxx4533xxlovexxdfd'
s4 = re.search('xx(.?)xx4533xx(.?)xx', s3).group(2)
print(s4)#提取出来

findall 的区别

s6 = re.findall('xx(.?)xx4533xx(.?)xx', s3)
print(s6[0][1])

s5 = '123abcssfasfefef123'
output = re.sub('123(.*?)123', '18010099819', s5)
print(output)

info = re.findall('xx(.*?)xx', search_cod, re.S)
for each in info:
print(each)

不要使用complie 这种是先编译, 后匹配,多此一举

pattern = 'xx(.*?)xx'
new_pattern = re.compile(pattern, re.S)
output2 = re.findall(new_pattern, search_cod)
print(output2)

匹配数字

s6 = 'fefefe180100998819fefef'
s7 = re.findall('(\d+)', s6)
print(s7)

你可能感兴趣的:(正则表达式)