Python中的正则表达式模块re
提供了许多常用的方法和功能,用于匹配和处理文本。以下是一些常用的方法和示例:
re.match(pattern, string)
:从字符串的开始位置匹配指定的正则表达式模式,如果匹配成功,返回一个匹配对象,否则返回None。示例:
import re
string = "Hello, World!"
match = re.match(r'Hello', string)
if match:
print("Matched!") # Matched!
re.search(pattern, string)
:在整个字符串中搜索指定的正则表达式模式,如果匹配成功,返回第一个匹配对象,否则返回None。示例:
import re
string = "Hello, World! Welcome to Python."
match = re.search(r'Python', string)
if match:
print("Found!") # Found!
re.findall(pattern, string)
:在字符串中搜索所有匹配正则表达式模式的子串,返回一个包含所有匹配结果的列表。示例:
import re
string = "apple,banana,orange,grape"
fruits = re.findall(r'\b\w+\b', string)
print(fruits) # ['apple', 'banana', 'orange', 'grape']
re.finditer(pattern, string)
:在字符串中搜索所有匹配正则表达式模式的子串,返回一个包含所有匹配对象的迭代器。示例:
import re
string = "Hello, World! Welcome to Python."
for match in re.finditer(r'\w+', string):
print(match.group()) # Hello, World, Welcome, Python
re.sub(pattern, repl, string)
:在字符串中使用正则表达式模式匹配并替换子串,返回替换后的字符串。示例:
import re
string = "Hello, 123 World!"
new_string = re.sub(r'\d+', 'XYZ', string)
print(new_string) # Hello, XYZ World!
re.subn(pattern, repl, string)
:匹配字符并替换,返回元组类型(替换后的字符串,被替换次数)。示例:
import re
string = "Hello, World!"
new_string, count = re.subn(r'o', 'x', string)
print(new_string) # Hellx, Wxrld!
print(count) # 2
re.split(pattern, string)
:根据正则表达式模式将字符串拆分为子串,并返回一个列表。示例:
import re
string = "apple,banana,orange,grape"
fruits = re.split(r',', string)
print(fruits) # ['apple', 'banana', 'orange', 'grape']
re.finditer(pattern, string)
:在字符串中搜索所有匹配正则表达式模式的子串,返回一个包含所有匹配对象的迭代器。示例:
import re
string = "Hello, World! Welcome to Python."
for match in re.finditer(r'\w+', string):
print(match.group()) # Hello, World, Welcome, Python
re.compile(pattern)
:编译正则表达式模式,返回一个可重复使用的正则表达式对象。示例:
import re
pattern = r'\d+'
regex_object = re.compile(pattern)
match = regex_object.match('12345')
if match:
print("Matched digit:", match.group()) # Matched digit: 12345
re.escape(string)
:在字符串中转义特殊字符,返回转义后的字符串。示例:
import re
string = 'Hello, *.py'
escaped_string = re.escape(string)
print(escaped_string) # Hello\, \*.py
re.purge()
:清除正则表达式模块中的所有编译过的模式,释放内存。示例:
import re
re.purge()