您好,登录后才能下订单哦!
正则表达式(Regular Expression,简称 regex 或 regexp)是一种强大的文本处理工具,广泛应用于字符串匹配、搜索、替换等操作。Python 通过 re
模块提供了对正则表达式的支持。本文将介绍 Python 中常用的正则表达式语法,帮助你快速掌握其基本用法。
正则表达式是由普通字符(如字母、数字)和特殊字符(称为元字符)组成的字符串模式。通过这种模式,可以匹配、查找或替换文本中的特定内容。
在 Python 中,使用 re
模块来操作正则表达式。以下是一个简单的示例:
import re
# 匹配字符串中的 "hello"
pattern = r"hello"
text = "hello world"
match = re.search(pattern, text)
if match:
print("找到匹配:", match.group())
else:
print("未找到匹配")
元字符是正则表达式中具有特殊意义的字符。以下是常用的元字符及其功能:
.
:匹配任意单个字符(除换行符 \n
外)。\d
:匹配任意数字(等价于 [0-9]
)。\D
:匹配任意非数字字符。\w
:匹配任意字母、数字或下划线(等价于 [a-zA-Z0-9_]
)。\W
:匹配任意非字母、数字或下划线的字符。\s
:匹配任意空白字符(包括空格、制表符、换行符等)。\S
:匹配任意非空白字符。[]
:匹配括号内的任意一个字符。例如,[abc]
匹配 a
、b
或 c
。[^]
:匹配不在括号内的任意字符。例如,[^abc]
匹配除 a
、b
、c
之外的字符。-
:在字符集合中表示范围。例如,[a-z]
匹配任意小写字母。*
:匹配前面的字符 0 次或多次。+
:匹配前面的字符 1 次或多次。?
:匹配前面的字符 0 次或 1 次。{n}
:匹配前面的字符恰好 n 次。{n,}
:匹配前面的字符至少 n 次。{n,m}
:匹配前面的字符至少 n 次,至多 m 次。^
:匹配字符串的开头。$
:匹配字符串的结尾。\b
:匹配单词边界。\B
:匹配非单词边界。()
:将括号内的内容分组,并捕获匹配的内容。(?:)
:非捕获分组,匹配但不捕获内容。|
:表示“或”关系。例如,a|b
匹配 a
或 b
。Python 的 re
模块提供了以下常用函数:
re.search()
在字符串中搜索匹配正则表达式的第一个位置,返回一个匹配对象(Match
对象)。如果未找到匹配,则返回 None
。
import re
pattern = r"\d+"
text = "There are 123 apples."
match = re.search(pattern, text)
if match:
print("找到数字:", match.group()) # 输出: 123
re.match()
从字符串的开头开始匹配正则表达式。如果开头不匹配,则返回 None
。
import re
pattern = r"\d+"
text = "123 apples."
match = re.match(pattern, text)
if match:
print("找到数字:", match.group()) # 输出: 123
re.findall()
返回字符串中所有匹配正则表达式的子串,结果以列表形式返回。
import re
pattern = r"\d+"
text = "There are 123 apples and 456 oranges."
matches = re.findall(pattern, text)
print("找到的数字:", matches) # 输出: ['123', '456']
re.sub()
用指定的字符串替换匹配正则表达式的子串。
import re
pattern = r"\d+"
text = "There are 123 apples."
result = re.sub(pattern, "XXX", text)
print("替换后的文本:", result) # 输出: There are XXX apples.
re.split()
根据正则表达式匹配的子串分割字符串,返回分割后的列表。
import re
pattern = r"\s+"
text = "Split this text by spaces."
result = re.split(pattern, text)
print("分割后的列表:", result) # 输出: ['Split', 'this', 'text', 'by', 'spaces.']
正则表达式默认是贪婪匹配,即尽可能多地匹配字符。可以通过在量词后加 ?
实现非贪婪匹配。
import re
pattern = r"\d+"
text = "12345"
match = re.search(pattern, text)
print("贪婪匹配:", match.group()) # 输出: 12345
import re
pattern = r"\d+?"
text = "12345"
match = re.search(pattern, text)
print("非贪婪匹配:", match.group()) # 输出: 1
import re
pattern = r"[\w\.-]+@[\w\.-]+"
text = "Contact us at support@example.com."
match = re.search(pattern, text)
if match:
print("找到邮箱:", match.group()) # 输出: support@example.com
import re
pattern = r"https?://[\w\.-]+"
text = "Visit https://www.example.com for more info."
match = re.search(pattern, text)
if match:
print("找到 URL:", match.group()) # 输出: https://www.example.com
import re
pattern = r"(bad|naughty|evil)"
text = "This is a bad example."
result = re.sub(pattern, "***", text)
print("替换后的文本:", result) # 输出: This is a *** example.
Python 的正则表达式功能强大且灵活,能够高效处理复杂的文本匹配任务。通过掌握常用的元字符、量词、分组以及 re
模块的函数,你可以轻松应对各种字符串处理需求。在实际开发中,正则表达式常用于数据清洗、日志分析、表单验证等场景。
希望本文能帮助你快速入门 Python 正则表达式!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。