1.split 函數(shù)
分割字符串產(chǎn)生一個(gè)列表
?re.split(pattern,str)
import re
str='helllo word'
print(re.split(r' ',str))
2. sub 函數(shù)
?替換
re.sub(pattern,,repl,string,count=0,flags=o)
print(re.sub('word','world',str))
3. subn函數(shù)
?替換并顯示次數(shù)
?re.subn(pattern,repel,str[,count])
s='jintian tianqi '
print(re.subn('i','l',s))
4. re中的屬性和方法
import re
print(re.__all__)
help(re.fullmatch)
5.其他
# re.I:忽略大小寫
# re.L:本地化識別匹配
# re.M:多行匹配
# re.S:匹配所有字符串,包括換行符
# re.U:根據(jù)Unicode解析字符串
# re.X:忽略空格和#后面的內(nèi)容
import re
se='this is is sis is a island'
print(re.sub('is',' ',se,2))
print(re.sub(r'\bis\b',' ',se,2))
print(re.sub('T',' ',se,0,re.I))
# split
print(re.split(' ',se))
print(re.split(r'\s',se))
6. compile 生成正則表達(dá)式
?re.compile(pattern,[,flags])
smail='我的QQ郵箱是email:1667559226@qq.com.; 電話是18061763306'
p = re.compile(r'email:(.+?)\.;',)
print(re.findall(p,smail))
print(p.findall(smail))
7. fullmatch函數(shù)
?返回所有匹配值(什么叫字符完全匹配)
print(re.fullmatch('is',se))
print(re.fullmatch('this is is sis is a island',se))
8. escape函數(shù)
?轉(zhuǎn)義
?re.escape(pattern)
s2='http://baidu.com'
print(re.escape(s2))
op = ['+','-','*','\\']
for i in op:
? ? ? print(re.escape(i))
9. finditer
?生成一個(gè)匹配值的迭代器器
?re.finditer
s3=re.finditer('is',se)
print(s3)
print(type(s3))
for i in s3:
? ? print(i)
10.拓展庫
?request
存放所有的網(wǎng)址
def allurl():
? ? html=[]
? ? for i in range(10):
? ? ? ? url="https://movie.douban.com/top250?start=%d&filter="%(i*25)
? ? ? ? html.append(url)
? ? return html