python五十七課——正則表達式(邊界字符)
阿新 • • 發佈:2019-03-10
... sea 一行 clas str1 則表達式 arc col lag
演示匹配錨字符(邊界字符)
^:從字符串頭部開始匹配,在開啟多行模式下(re.M),可以嘗試匹配每一行的頭部數據
$:從字符串尾部開始匹配,在開啟多行模式下(re.M),可以嘗試匹配每一行的尾部數據
A:從字符串頭部開始匹配,在開啟多行模式下(re.M),沒有多行的概念,還是匹配第一行的頭
Z:從字符串尾部開始匹配,在開啟多行模式下(re.M),沒有多行的概念,還是匹配最後一行的尾
:匹配邊界(左、右),如果滿足返回對象(match、list)
B:先舍棄邊界數據,然後一定是從左側開始匹配...
import re print(re.search(r‘^www‘,‘hahawww.baidu.comhehe‘)) print(re.search(r‘^haha‘,‘hahawww.baidu.comhehe‘)) print(re.search(r‘hehe$‘,‘hahawww.baidu.comhehe‘)) print(re.search(r‘com$‘,‘hahawww.baidu.comhehe‘)) print(re.search(r‘Awww‘,‘hahawww.baidu.comhehe‘)) print(re.search(r‘Ahaha‘,‘hahawww.baidu.comhehe‘)) print(re.search(r‘heheZ‘,‘hahawww.baidu.comhehe‘)) print(re.search(r‘comZ‘,‘hahawww.baidu.comhehe‘)) print(‘驗證匹配字符串首位字符的區別:‘) str1=‘‘‘ hank is a cool man hank is a nice man hank is a prefect man ‘‘‘ print(re.findall(r‘hank‘,str1,flags=re.M)) print(re.findall(r‘leo‘,str1,flags=re.M)) print(re.findall(r‘man$‘,str1,flags=re.M)) print(re.findall(r‘manZ‘,str1,flags=re.M)) print(re.search(r‘ne‘,‘never‘)) print(re.search(r‘ne?‘,‘never‘)) print(re.search(r‘er?‘,‘never‘)) print(re.search(r‘erB‘,‘never‘)) print(re.search(r‘erB‘,‘nerver‘)) print(re.search(r‘erB‘,‘ernerver‘)) print(re.search(r‘Ber‘,‘ernerver‘))
python五十七課——正則表達式(邊界字符)