1. 程式人生 > >Python正則RE

Python正則RE

asc repl 分割 貪婪 不包含 字符 十進制數 .cn wwwn

就其本質而言,正則表達式(或 RE)是一種小型的、高度專業化的編程語言,(在Python中)它內嵌在Python中,並通過 re 模塊實現。正則表達式模式被編譯成一系列的字節碼,然後由用 C 編寫的匹配引擎執行。

RE字符:

  元字符:. ^ $ * + ? { } [ ] | ( ) \

  普通字符:字母數字

 1 import re
 2  
 3 ret=re.findall(a..in,helloalvin)   #.匹配一個字符
 4 print(ret)#[alvin]
 5  
 6  
 7 ret=re.findall(^a...n,alvinhelloawwwn
) #^從開頭匹配 8 print(ret)#[alvin] 9 10 11 ret=re.findall(a...n$,alvinhelloawwwn)#$從末尾匹配 12 print(ret)#[awwwn] 13 14 15 ret=re.findall(a...n$,alvinhelloawwwn) 16 print(ret)#[awwwn] 17 18 19 ret=re.findall(abc*,abcccc)#貪婪匹配[0,+oo] *前面的字母有0個或者多個 20 print(ret)#[abcccc] 21 22
ret=re.findall(abc+,abccc)#[1,+oo] +前面的字母有0個或者多個 23 print(ret)#[abccc] 24 25 ret=re.findall(abc?,abccc)#[0,1] ?前面的字母1個或者0個 26 print(ret)#[abc] 27 28 29 ret=re.findall(abc{1,4},abccc) 30 print(ret)#[abccc] 貪婪匹配

註意:前面的*,+,?等都是貪婪匹配,也就是盡可能匹配,後面加?號使其變成惰性匹配

ret=re.findall(qta*?
,qtaaaasd) print(ret) #[qt]

元字符之字符集[]:

ret=re.findall(a[bc]d,abdacd)
print(ret)  #[abd, acd]
ret=re.findall(‘a[a-z]d‘,‘abdacd‘)
print(ret) #[‘abd‘, ‘acd‘]

ret=re.findall(‘[.*,]‘,‘ab,da.c*d‘)   
print(ret)#[‘,‘, ‘.‘, ‘*‘]

ret=re.findall(‘[^.*,]‘,‘ab,da.c*d‘)  #不包含字符集內的元素
print(ret) # [‘a‘, ‘b‘, ‘d‘, ‘a‘, ‘c‘, ‘d‘]

ret=re.findall(‘[\d]‘,‘ab,da.c*d123‘)  #\d匹配任何數字 和[1-9]一樣
print(ret) #[‘1‘, ‘2‘, ‘3‘]
ret=re.findall(‘[\D]‘,‘ab,da.c*d123‘) #\D匹配所有不是數字的字符
print(ret)# [‘a‘, ‘b‘, ‘,‘, ‘d‘, ‘a‘, ‘.‘, ‘c‘, ‘*‘, ‘d‘]

ret=re.findall(‘[\w]‘,‘ab,da.c*d123‘)#\w匹配任何數字和字符類似[1-9a-zA-Z]
print(ret) #[‘a‘, ‘b‘, ‘d‘, ‘a‘, ‘c‘, ‘d‘, ‘1‘, ‘2‘, ‘3‘]
ret=re.findall(‘[\W]‘,‘ab,da.c*d123‘) #\W匹配任何不是數字和字符的
print(ret) #[‘,‘, ‘.‘, ‘*‘]
ret=re.findall(‘[\s]‘,‘ab,da.c*d123\t\n‘) #\s匹配任何空白字符
print(ret) #[‘\t‘, ‘\n‘]
ret=re.findall(‘[\S]‘,‘ab,da.c*d123\t\n‘) #\S匹配任何不是空白字符
print(ret) #[‘a‘, ‘b‘, ‘,‘, ‘d‘, ‘a‘, ‘.‘, ‘c‘, ‘*‘, ‘d‘, ‘1‘, ‘2‘, ‘3‘]


元字符之轉義符\

反斜杠後邊跟元字符去除特殊功能,比如\.
反斜杠後邊跟普通字符實現特殊功能,比如\d

\d 匹配任何十進制數;它相當於類 [0-9]。
\D 匹配任何非數字字符;它相當於類 [^0-9]。
\s 匹配任何空白字符;它相當於類 [ \t\n\r\f\v]。
\S 匹配任何非空白字符;它相當於類 [^ \t\n\r\f\v]。
\w 匹配任何字母數字字符;它相當於類 [a-zA-Z0-9_]。
\W 匹配任何非字母數字字符;它相當於類 [^a-zA-Z0-9_]
\b 匹配一個特殊字符邊界,比如空格 ,&,#等

現在我們聊一聊\,先看下面兩個匹配:

 1 #-----------------------------eg1:
 2 import re
 3 ret=re.findall(c\l,abc\le)
 4 print(ret)#[]
 5 ret=re.findall(c\\l,abc\le)
 6 print(ret)#[]
 7 ret=re.findall(c\\\\l,abc\le)
 8 print(ret)#[c\\l]
 9 ret=re.findall(rc\\l,abc\le)
10 print(ret)#[c\\l]  r說明再python用原生字符串
11  
12 #-----------------------------eg2:
13 #之所以選擇\b是因為\b在ASCII表中是有意義的
14 m = re.findall(\bblow, blow)
15 print(m)
16 m = re.findall(r\bblow, blow)
17 print(m)

技術分享

元字符之分組()

1 m = re.findall((bl), blow)  #整體匹配‘bl’
2 print(m)  #[bl]

元字符之|

ret=re.search((ab)|\d,rabhdg8sd)  #或,從左開始匹配,如果有匹配出來,如果沒有匹配右邊的
print(ret.group())#ab

RE的五種基本操做

re.match(pattern, string)     # 從頭匹配

re.search(pattern, string)    # 匹配整個字符串,直到找到一個匹配

re.split()            # 將匹配到的格式當做分割點對字符串分割成列表
>>>m = re.split("[0-9]", "alex1rain2jack3helen rachel8")
>>>print(m)
輸出: [alex, rain, jack, helen rachel, ‘‘]

re.findall()          # 找到所有要匹配的字符並返回列表格式

>>>m = re.findall("[0-9]", "alex1rain2jack3helen rachel8")
>>>print(m)<br>
輸出:[1, 2, 3, 8]

re.sub(pattern, repl, string, count,flag)    # 替換匹配到的字符
m=re.sub("[0-9]","|", "alex1rain2jack3helen rachel8",count=2 )
print(m)
輸出:alex|rain|jack3helen rachel8  

Python正則RE