一、基礎語法總結
1.1、匹配單個字符
a . d D w W s S [...] [^...]
匹配單個字符(.)
規則:匹配除換行之外的任意字符
In [24]: re.findall("f.o","foo is not fao")
Out[24]: ['foo', 'fao']
匹配任意(非)數字字符(d D)
d [0-9]
D [^0-9]
匹配任意(非)普通字符(w W)
w 普通字符 包括[_0-9A-Za-z] 同時也包括漢字
W 非普通字符
匹配任意(非)空字符(s S)
s 匹配任意空字符 [
]
S 匹配任意非空字符
匹配字符集合([...])
[A-Z][a-z][0-9][_123a-z]
匹配字符集([^...])
規則:字符集取非,除列出的字符之外的任意一個字符
[^abc] --> 除a b c之外任意字符
1.2、匹配多個字符
* 匹配0次或者多次
+ 匹配1次或者多次
? 匹配0次或者1次
匹配m次
匹配m次到n次區間內的任意一次
1.3、匹配位置
^ 匹配開始位置
$ 匹配結束位置
A 匹配開始位置
Z 匹配結束位置
匹配單詞邊界位置(一般用於首字母大寫的匹配)
B 匹配非單詞邊界問題
1.4、轉義
在正則表達式中有一類特殊字符需要轉移,只需要在特殊字符之間加上 表示轉移即可
. * + ? ^ $ [] {} () |
1.5、子組
使用() 可以為正則表達式建立內部分組,子組為正則表達式的一部分,可以看做一個內部整體。
In [61]: re.search(r"(https|http|ftp)://w+.w+.(com|cn)","https://www.baidu.com").group(0)
Out[61]: 'https://www.baidu.com'
In [62]: re.search(r"(https|http|ftp)://w+.w+.(com|cn)","https://www.baidu.com").group(1)
Out[62]: 'https'
1.6、貪婪模式和非貪婪模式
正則表達式的重複匹配總是儘可能多的向後匹配更多的內容。貪婪模式包括:* + ?
非貪婪模式:儘可能少的匹配內容 貪婪模式轉換為非貪婪模式:*? +? ?? ?
In [106]: re.findall(r"ab+?","abbbbbbbb")
Out[106]: ['ab']
In [107]: re.findall(r"ab??","abbbbbbbb")
Out[107]: ['a']
二、Re模塊
接下來我所有函數裡面的參數解釋如下:
pattern:正則表達式
string:目標字符串
pos:截取目標字符串起始位置
endpose:截取目標字符串結束位置
flags:功能標誌
replaceStr:替換的字符串
max:最多替換幾處(默認替換全部)
有上圖我們看出來,接下來我們要將的Python中re模塊、regex對象、match對象三者之間是存在一定關係的。
1、re模塊的compile方法返回一個regex對象
2、re模塊和regex對象的finditer()、fullmatch()、match()、search()等方法返回一個match對象
3、他們分別有自己的屬性和方法
2.1、compile
regex = re.compile(pattern, flags = 0) # 生成正則表達式對象
2.2、findall
re.findall(pattern,string,pos,endpose) # 從目標字符串中匹配所有符合條件的內容
2.3、split
re.split(pattern,string,flags) #根據正則表達式對目標字符串進行分割
In [79]: re.split(r's+',"Hello World")
Out[79]: ['Hello', 'World']
2.4、sub
re.sub(pattern,replaceStr,string,max,flags)
In [80]: re.sub(r's+',"##","hello world")
Out[80]: 'hello##world'
2.5、subn
re.subn(pattern,replaceStr,string,max,flags) #功能同sub,但是返回值返回替換後的字符串和替換了幾處
In [80]: re.sub(r's+',"##","hello world")
Out[80]: ('hello##world',1)
2.6、finditer
re.finditer(pattern,string) #使用正則表達式匹配目標字符串,返回一個match對象,match對象調用group()之後才能拿到值
In [87]: it = re.finditer(r'd+',"2014nianshiqiqngduo 08aoyun 512dizhen")
In [88]: for i in it:
....: print(i)
....:
In [93]: it = re.finditer(r'd+',"2014nianshiqiqngduo 08aoyun 512dizhen")
In [94]: for i in it:
....: print(i.group())
....:
2014
08
512
2.7、fullmatch
fullmatch(pattern,string,flags) #完全匹配目標字符串,相當於加了^ 和 $
2.8、match
re.match(pattern,string,flags) #匹配目標字符串開頭的位置
2.9、search
re.search(pattern,string,flags) # 正則表達式匹配目標字符串,只匹配第一處
三、一些練習題
3.1、匹配首字母大寫的單詞
import re
f = open('test.txt')
pattern= r'[A-Z][a-zA-Z]*s*'
# pattern= r'[A-Z]S'
L = []
for i in f:
L += re.findall(pattern,i)
print(L)
test.txt文檔內容如下:
Hello World -12.6
Nihao 123
How are you -12
1.24
asdk 34%,
佔比 1/2
2003 - 2005./%
3.2、匹配數字(正數、負數、小數、百分數、分數)
import re
pattern = "-?d+((/?d+)|((.)?d+)|((%)?))"
f = open('test.txt')
l = []
for line in f:
l += re.finditer(pattern,line)
for i in l:
print(i.group())
希望大家可以多多關注,評論,轉發,收藏,您的支持是對小編最大的鼓勵!
想要擁有更加牛逼的技巧嗎?
想要了解最新的行業信息嗎?
想要獲得老師的經驗分享嗎?
目前10000+人已加入我們
現在點擊閱讀全文報名,即享0元課程優惠!快來搶購吧!
領取福利加助理夢雅微信:762459510 備註:聽課
點擊下方,領取課程
掃碼二維碼領取Python、爬蟲、自動化學習基礎入門實戰試學課程+課程諮詢+福利領取!