Python进阶-正则表达式
正则表达式re库
正则表达式(Regular Expressions)是用来匹配字符串中字符组合的强大工具。
正则表达式由普通字符(例如字母和数字)和特殊字符(称为元字符)组成。元字符用于定义搜索模式。
常见的正则元字符
- .:匹配任何单个字符(除了换行符)。
- ^:匹配字符串的开头。
- $:匹配字符串的结尾。
- *:匹配前一个元素零次或多次。
- +:匹配前一个元素一次或多次。
- ?:匹配前一个元素零次或一次。
- {n}:匹配前一个元素恰好n次。
- {n,}:匹配前一个元素至少n次。
- {n,m}:匹配前一个元素至少n次,但不超过m次。
- []:定义一个字符类,匹配方括号内的任意字符。
- |:表示“或”操作。
- ():用于分组。
re模块使用方法
安装re模块:
pip install re
使用方法:
# 导入库
import re
"""
常用方法:
.match() 从字符串的开头匹配模式。
.search() 在整个字符串中搜索模式。
.findall() 返回字符串中所有匹配的子串。
.finditer() 返回一个迭代器,生成所有匹配的对象。
.sub() 替换字符串中匹配的部分。
.split() 按照模式分割字符串。
"""
text = 'hello, every one!!!'
if re.match(r'hello', text):
print(f'成功匹配内容以hello开头,内容: {text}')
使用实例
import re
text = r'Hello, my name is Bob and my email is example@email.com, And my phone number is 123-456-7890. I enjoy playing basketball and watching movies, and I hope to become friends with you.'
# 匹配开头是不是Hello
match = re.match(r'Hello', text)
if match:
print("Match found:", match.group())
# 搜索email is后面的邮箱地址
search = re.search(r'email is (.+?) ', text)
if search:
print("Email found:", search.group(1))
# 找到所有的 my 字段,统计有多少个
is_my = re.findall(r'my', text)
print('匹配的总数为:', len(is_my))
# 找到所有的匹配对象为邮箱地址
for match in re.finditer(r'\b\w+@\w+\.\w+\b', text):
print("Found email:", match.group())
# 替换手机号
new_text = re.sub(r'\d{3}-\d{3}-\d{4}', 'XXX-XXX-XXXX', text)
print("Text after substitution:", new_text)
# 找出标点符号进行分割字符串
split_text = re.split(r'[\s,.]+', text)
print("Split text:", split_text)