什么是正則表達式?
Python爬蟲在獲得網頁的content之后,要通過一系列解析和查找去提取你想要的信息,這個時候正則表達式就起到了很大的作用。
正則表達式是對字符串操作的一種邏輯公式,就是事先定義好的一些特定字符、及這些特定字符的組合,組成 個“規則字符”,這個“規則字符” 來表達對字符的種過濾邏輯。
正則并不是python獨有的,其他語言也都有正則。python中的正則,封裝在了re模塊。
常用的匹配模式
\w 匹配字母數字及下劃線
\W 匹配f非字母數字下劃線
\s 匹配任意空白字符,等價于[\t\n\r\f]
\S 匹配任意非空字符
\d 匹配任意數字
\D 匹配任意非數字
\A 匹配字符串開始
\Z 匹配字符串結束,如果存在換行,只匹配換行前的結束字符串
\z 匹配字符串結束
\G 匹配最后匹配完成的位置
\n 匹配一個換行符
\t 匹配一個制表符
^ 匹配字符串的開頭
\$ 匹配字符串的末尾
. 匹配任意字符,除了換行符,re.DOTALL標記被指定時,則可以匹配包括換行符的任意字符
[....] 用來表示一組字符,單獨列出:[amk]匹配a,m或k
[^...] 代表不在[]中的字符,[^abc]匹配除了a,b,c之外的字符
*匹配0個或多個的表達式
+匹配1個或者多個的表達式
?匹配0個或1個由前面的正則表達式定義的片段,非貪婪方式
{n} 精確匹配n前面的表示
{m,m} 匹配n到m次由前面的正則表達式定義片段,貪婪模式
a|b 匹配a或者b
() 匹配括號內的表達式,也表示一個組
re.match()
嘗試從字符串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就會返回None。語法格式:re.match(pattern,string,flags=0)
import re
content= "hello 123 4567 World_This is a regex Demo"
result = re.match('^hello\s\d\d\d\s\d{4}\s\w{10}.*Demo\$',content)
print(result)
print(result.group())
print(result.span())
- result.group()獲取匹配的結果
- result.span()獲去匹配字符串的長度范圍
泛匹配
其實相對來說上面的方式并不是非常方便,其實可以將上述的正則規則進行更改:
import re
content= "hello 123 4567 World_This is a regex Demo"
result = re.match("^hello.*Demo$",content)
print(result)
print(result.group())
print(result.span())
這段代碼的結果和上面常規匹配的結果是一樣的,但是寫起來會方便很多。
匹配目標
如果為了匹配字符串中具體的目標,則需要通過()
括起來,通過re.group()獲得結果后,如果正則表達式中有括號,則re.group(1)獲取的就是第一個括號中匹配的結果。例子如下:
import re
content= "hello 1234567 World_This is a regex Demo"
result = re.match('^hello\s(\d+)\sWorld.*Demo$',content)
print(result)
print(result.group())
print(result.group(1))
print(result.span())
貪婪匹配
import re
content= "hello 1234567 World_This is a regex Demo"
result= re.match('^hello.*(\d+).*Demo',content)
print(result)
print(result.group(1))
result.group(1)會返回7而不是1234567,這是因為默認是貪婪匹配,前面的.*
會盡可能多的匹配字符。如果想得到1234567,則需要改為result= re.match('^he.*?(\d+).*Demo',content)
,這樣結果就可以匹配到1234567。
匹配模式
很多時候匹配的內容是存在換行的問題的,這個時候的就需要用到匹配模式re.S來匹配換行的內容。
import re
content = """hello 123456 world_this
my name is jamesqiu
"""
result =re.match('^he.*?(\d+).*?jamesqiu$',content,re.S)
print(result)
print(result.group())
print(result.group(1))
轉義
當我們要匹配的內容中存在特殊字符的時候,就需要用到轉移符號,例子如下:
import re
content= "price is $5.00"
result = re.match('price is \$5\.00',content)
print(result)
print(result.group())
小結
- 盡量使用泛匹配,使用括號得到匹配目標,盡量使用非貪婪模式,有換行符就用re.S
- 強調re.match是從字符串的起始位置匹配一個模式
re.search
re.search掃描整個字符串返回第一個成功匹配的結果:
import re
content = "extra things hello 123455 world_this is a Re Extra things"
result = re.search("hello.*?(\d+).*?Re",content)
print(result)
print(result.group())
print(result.group(1))
其實這個時候我們就不需要在寫^以及$,因為search是掃描整個字符串。注意:所以為了匹配方便,我們會更多的用search,不用match,match必須匹配頭部,所以很多時候不是特別方便。
re.findall()
搜索字符串,以列表的形式返回全部能匹配的子串。
re.sub()
re.sub(正則表達式,替換成的字符串,原字符串)
import re
content = "Extra things hello 123455 World_this is a regex Demo extra things"
content = re.sub('\d+','',content)
print(content)
結果會將數字替換為為空。
re.compile()
將正則表達式編譯成正則表達式對象,方便復用該正則表達式。
import re
content= """hello 12345 world_this
fan
"""
pattern =re.compile("hello.*fan",re.S)
result = re.match(pattern,content)
print(result)
print(result.group())