一、re.match()函數的使用
在Python re模塊中,有一個常用函數re.match()用於匹配字元串的開頭是否符合正則表達式規則。具體使用方式是:
import re result = re.match(pattern, string)
其中,pattern表示正則表達式的規則,string表示要匹配的字元串,函數返回一個match對象,可以根據match對象的方法獲取匹配的結果。
二、匹配單個字元
在正則表達式中使用”.”表示可以匹配任意一個字元,下面是一個示例:
import re result = re.match(".", "hello") print(result.group()) # Output: "h"
上述代碼中,正則表達式”.”匹配”hello”字元串的第一個字元”h”,因此輸出結果為”h”。
三、匹配指定字符集
在正則表達式中使用”[]”表示可以匹配指定的字符集,下面是一個示例:
import re result = re.match("[abc]", "hello") print(result.group()) # Output: None result = re.match("[abc]", "bob") print(result.group()) # Output: "b"
上述代碼中,正則表達式”[abc]”匹配”hello”字元串中的任意一個字元,由於”hello”字元串的第一個字元不在指定的字符集中,因此result.group()返回None;而正則表達式”[abc]”匹配”bob”字元串的第一個字元”b”,因此輸出結果為”b”。
四、匹配字符集中的任意一個字元
在正則表達式中使用”|”表示可以匹配字符集中的任意一個字元,下面是一個示例:
import re result = re.match("[abc|def]", "hello") print(result.group()) # Output: None result = re.match("[abc|def]", "dog") print(result.group()) # Output: "d"
上述代碼中,正則表達式”[abc|def]”匹配”hello”字元串中的任意一個字元,由於”hello”字元串的第一個字元不在指定的字符集中,因此result.group()返回None;而正則表達式”[abc|def]”匹配”dog”字元串的第一個字元”d”,因此輸出結果為”d”。
五、匹配數字或字母
在正則表達式中使用”\d”表示可以匹配數字,使用”\w”表示可以匹配字母,下面是一個示例:
import re result = re.match("\d", "hello") print(result.group()) # Output: None result = re.match("\d", "123") print(result.group()) # Output: "1" result = re.match("\w", "_hello") print(result.group()) # Output: "_"
上述代碼中,正則表達式”\d”匹配”hello”字元串的第一個字元,由於該字元不是數字,因此result.group()返回None;而正則表達式”\d”匹配”123″字元串的第一個字元”1″,因此輸出結果為”1″。類似地,正則表達式”\w”匹配”_hello”字元串的第一個字元”_”,因此輸出結果為”_”。
六、匹配任意空白字元
在正則表達式中使用”\s”表示可以匹配任意空白字元,包括空格、製表符、換行符等等,下面是一個示例:
import re result = re.match("\s", "hello") print(result.group()) # Output: None result = re.match("\s", " hello") print(result.group()) # Output: " "
上述代碼中,正則表達式”\s”匹配”hello”字元串的第一個字元,由於該字元不是空白字元,因此result.group()返回None;而正則表達式”\s”匹配” hello”字元串的第一個字元空格” “,因此輸出結果為” “。
七、匹配重複出現的字元
在正則表達式中使用”*”、”+”、”?”、”{m}”和”{m,n}”等符號表示可以匹配相應字元或字符集的重複出現,下面是幾個示例:
import re result = re.match("hi*", "hiii") print(result.group()) # Output: "hiii" result = re.match("hi*", "hello") print(result.group()) # Output: "h" result = re.match("hi+", "hello") print(result.group()) # Output: None result = re.match("hi+", "hiiii") print(result.group()) # Output: "hiiii" result = re.match("hi?", "hello") print(result.group()) # Output: "h" result = re.match("hi{2}", "hello") print(result.group()) # Output: None result = re.match("hi{2}", "hii") print(result.group()) # Output: "hii" result = re.match("hi{2,3}", "hi") print(result.group()) # Output: None result = re.match("hi{2,3}", "hii") print(result.group()) # Output: "hii" result = re.match("hi{2,3}", "hiii") print(result.group()) # Output: "hiii"
上述代碼中,每個正則表達式規則中都包含了”hi”字元串,但是使用了不同的符號匹配重複出現的字元。通過這些示例,可以了解到,”*”表示匹配0或多次,”+”表示匹配1或多次,”?”表示匹配0或1次,”{m}”表示匹配m次,”{m,n}”表示匹配m~n次。
八、匹配非數字字元
在正則表達式中使用”\D”表示可以匹配非數字字元,下面是一個示例:
import re result = re.match("\D", "hello") print(result.group()) # Output: "h" result = re.match("\D", "123") print(result.group()) # Output: None
上述代碼中,正則表達式”\D”匹配”hello”字元串的第一個字元”h”,由於該字元不是數字字元,因此輸出結果為”h”;而正則表達式”\D”匹配”123″字元串的第一個字元”1″,由於該字元是數字字元,因此result.group()返回None。
九、總結
本文介紹了Python re.match()函數的使用以及一些常見的正則表達式規則,在實際工作中可以根據實際情況使用不同的正則表達式規則進行匹配操作。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237432.html