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 的區(qū)別
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)
匹配數(shù)字
s6 = 'fefefe180100998819fefef'
s7 = re.findall('(\d+)', s6)
print(s7)