正则表达式(Regular Expressions,简称 regex)是文本处理和字符串操作中非常强大的工具。在Python中,正则表达式主要通过re
模块来实现。掌握正则表达式的关键操作符,可以帮助我们更高效地进行文本处理。本文将详细介绍Python正则表达式的关键操作符,帮助读者解锁文本处理的秘密技巧。
1. 常用元字符
元字符是正则表达式中具有特殊含义的字符,用于匹配特定的字符集或模式。以下是一些常用的元字符:
1.1 点号(.)
点号.
匹配除换行符以外的任意单个字符。
import re
pattern = r'.'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: [' ', 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
1.2 星号(*)
星号*
匹配前面的子表达式零次或多次。
pattern = r'.*'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: ['hello world']
1.3 加号(+)
加号+
匹配前面的子表达式一次或多次。
pattern = r'.+'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: ['hello world']
1.4 问号(?)
问号?
匹配前面的子表达式零次或一次。
pattern = r'.?'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
1.5 花括号({})
花括号{}
用于指定匹配次数。
pattern = r'.{3}'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: ['hel', 'lo ', 'wor']
1.6 方括号([])
方括号[]
用于匹配一组字符中的任意一个。
pattern = r'[aeiou]'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: ['e', 'o']
1.7 脱字符(^)
脱字符^
匹配字符串的开始位置。
pattern = r'^hello'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: ['hello']
1.8 美元符号($)
美元符号$
匹配字符串的结束位置。
pattern = r'world$'
text = "hello world"
result = re.findall(pattern, text)
print(result) # 输出: ['world']
2. 常用函数和方法
Python中的re
模块提供了许多用于处理正则表达式的函数和方法。以下是一些常用的函数和方法:
2.1 re.match()
re.match()
函数从字符串的起始位置匹配正则表达式,如果匹配成功返回一个匹配对象,否则返回None
。
import re
pattern = r'd'
text = '123abc'
match = re.match(pattern, text)
if match:
print(match.group()) # 输出: 123
else:
print("No match")
2.2 re.search()
re.search()
函数扫描整个字符串,返回第一个匹配成功的匹配对象,否则返回None
。
import re
pattern = r'd'
text = 'abc123def456'
match = re.search(pattern, text)
if match:
print(match.group()) # 输出: 123
else:
print("No match")
2.3 re.findall()
re.findall()
函数返回所有匹配正则表达式的子串列表。
import re
pattern = r'd'
text = 'abc123def456'
result = re.findall(pattern, text)
print(result) # 输出: ['123', '456']
2.4 re.finditer()
re.finditer()
函数返回一个迭代器,迭代匹配对象。
”`python import re
pattern = r’d’ text = ‘abc123def456’ matches =