一、正則表達式的基礎知識
正則表達式是一種描述字元串特徵的語法規則,它可以幫助我們匹配、搜索、替換字元串。
在Python中,使用re模塊可以輕鬆地編寫正則表達式程序。
下面是一些正則表達式的基本符號:
. 匹配任意字元 ^ 匹配字元串的開頭 $ 匹配字元串的結尾 * 匹配前一個字元0次或多次 + 匹配前一個字元1次或多次 ? 匹配前一個字元0次或1次 {m} 匹配前一個字元m次 {m,n} 匹配前一個字元m到n次
二、python中re模塊常用函數
re模塊提供了很多函數用於字元串匹配與替換。
1. re.search(pattern, string)
在字元串中查找符合pattern的子串,返回一個匹配對象。
import re string = "hello world" pattern = r"world" match = re.search(pattern, string) if match: print("Matched!") else: print("Not matched!")
2. re.match(pattern, string)
在字元串的開頭查找符合pattern的子串,返回一個匹配對象。
import re string = "hello world" pattern = r"hello" match = re.match(pattern, string) if match: print("Matched!") else: print("Not matched!")
3. re.findall(pattern, string)
查找字元串中所有符合pattern的子串,返回一個列表。
import re string = "hello world" pattern = r"l" matches = re.findall(pattern, string) print(matches)
4. re.sub(pattern, repl, string)
替換字元串中的符合pattern的子串為repl。
import re string = "hello world" pattern = r"world" repl = "python" new_string = re.sub(pattern, repl, string) print(new_string)
三、常用的正則表達式實例
1. 匹配郵箱地址
郵箱地址的規則比較複雜,下面是一個基本的正則表達式,可以匹配大部分郵箱地址。
import re pattern = r"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}" string = "my email is example@example.com" match = re.search(pattern, string) print(match.group())
2. 匹配網址
下面是一個基本的正則表達式,可以匹配大部分網址。
import re pattern = r"(?i)http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+" string = "Visit my website: https://www.example.com" match = re.search(pattern, string) print(match.group())
3. 替換字元串中的數字
下面是一個正則表達式,可以將字元串中的數字替換成#。
import re pattern = r"\d+" string = "The price is $123.456." new_string = re.sub(pattern, "#", string) print(new_string)
四、總結
正則表達式是一種非常強大的工具,可以幫助我們輕鬆地完成字元串匹配與替換。在Python中,使用re模塊可以很方便地編寫正則表達式程序。掌握正則表達式的基礎知識和常用函數,可以有效地提高程序開發效率。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/301932.html