Python的re模塊是用于在字符串中進行正則表達式匹配和替換的模塊。以下是一些re模塊的常用函數和用法:
import re
pattern = r"hello"
string = "hello world"
result = re.match(pattern, string)
if result:
print("匹配成功")
else:
print("匹配失敗")
import re
pattern = r"world"
string = "hello world"
result = re.search(pattern, string)
if result:
print("匹配成功")
else:
print("匹配失敗")
import re
pattern = r"\d+"
string = "2019-07-01, 2020-01-01, 2021-05-01"
result = re.findall(pattern, string)
print(result)
import re
pattern = r"\d+"
string = "I have 3 apples and 5 oranges"
result = re.sub(pattern, "X", string)
print(result)
以上是re模塊的一些常用函數和用法,還有其他函數和用法可以參考Python官方文檔。