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官方文档。