正則表達式是Python程序設計中非常實用的功能,本文就常用的正則表達式做一匯總,供大家參考之用。具體如下:
一、字符串替換
1.替換所有匹配的子串
用newstring替換subject中所有與正則表達式regex匹配的子串
result, number = re.subn(regex, newstring, subject)
2.替換所有匹配的子串(使用正則表達式對象)
reobj = re.compile(regex)result, number = reobj.subn(newstring, subject)
二、字符串拆分
1.字符串拆分
result = re.split(regex, subject)
2.字符串拆分(使用正則表示式對象)
reobj = re.compile(regex)result = reobj.split(subject)
三、匹配
下面列出Python正則表達式的幾種匹配用法:
1.測試正則表達式是否匹配字符串的全部或部分
regex=ur"..." #正則表達式if re.search(regex, subject): do_something()else: do_anotherthing()
2.測試正則表達式是否匹配整個字符串
regex=ur".../Z" #正則表達式末尾以/Z結束if re.match(regex, subject): do_something()else: do_anotherthing()
3. 創建一個匹配對象,然后通過該對象獲得匹配細節
regex=ur"..." #正則表達式match = re.search(regex, subject)if match: # match start: match.start() # match end (exclusive): match.end() # matched text: match.group() do_something()else: do_anotherthing()
4.獲取正則表達式所匹配的子串
(Get the part of a string matched by the regex)regex=ur"..." #正則表達式match = re.search(regex, subject)if match: result = match.group()else: result = ""
5. 獲取捕獲組所匹配的子串
(Get the part of a string matched by a capturing group)regex=ur"..." #正則表達式match = re.search(regex, subject)if match: result = match.group(1)else: result = ""
6. 獲取有名組所匹配的子串
(Get the part of a string matched by a named group)regex=ur"..." #正則表達式match = re.search(regex, subject)if match: result = match.group("groupname")else: result = ""7. 將字符串中所有匹配的子串放入數組中
(Get an array of all regex matches in a string)result = re.findall(regex, subject)
8.遍歷所有匹配的子串
(Iterate over all matches in a string)for match in re.finditer(r"<(.*?)/s*.*?//1>", subject) # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()
9.通過正則表達式字符串創建一個正則表達式對象
(Create an object to use the same regex for many operations)reobj = re.compile(regex)
10.用法1的正則表達式對象版本
(use regex object for if/else branch whether (part of) a string can be matched)reobj = re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing()
11.用法2的正則表達式對象版本
(use regex object for if/else branch whether a string can be matched entirely)reobj = re.compile(r"/Z") #正則表達式末尾以/Z 結束if reobj.match(subject): do_something()else: do_anotherthing()
12.創建一個正則表達式對象,然后通過該對象獲得匹配細節
(Create an object with details about how the regex object matches (part of) a string)reobj = re.compile(regex)match = reobj.search(subject)if match: # match start: match.start() # match end (exclusive): match.end() # matched text: match.group() do_something()else: do_anotherthing()
13.用正則表達式對象獲取匹配子串
(Use regex object to get the part of a string matched by the regex)reobj = re.compile(regex)match = reobj.search(subject)if match: result = match.group()else: result = ""
14.用正則表達式對象獲取捕獲組所匹配的子串
(Use regex object to get the part of a string matched by a capturing group)reobj = re.compile(regex)match = reobj.search(subject)if match: result = match.group(1)else: result = ""
15.用正則表達式對象獲取有名組所匹配的子串
(Use regex object to get the part of a string matched by a named group)reobj = re.compile(regex)match = reobj.search(subject)if match: result = match.group("groupname")else: result = ""16.用正則表達式對象獲取所有匹配子串并放入數組
(Use regex object to get an array of all regex matches in a string)reobj = re.compile(regex)result = reobj.findall(subject)
17.通過正則表達式對象遍歷所有匹配子串
(Use regex object to iterate over all matches in a string)reobj = re.compile(regex)for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()
感興趣的讀者可以動手調試一下本文實例代碼,相信會有新的收獲。
新聞熱點
疑難解答
圖片精選