Python 中的正则表达式示例?

pythonserver side programmingprogramming更新于 2025/4/25 22:36:17

正则表达式是一种编程语言,用于识别给定字符序列(字符串)中是否存在某种模式。

正则表达式或 Regex 是用于检查字符串是否包含指定搜索模式的字符序列。

RegEx 模块

要使用 RegEx 模块,python 附带了名为 re 的内置包,我们需要使用正则表达式。要使用 RegEx 模块,只需导入 re 模块。

import re

示例

import re
txt = "Use of python in Machine Learning"
x = re.search("^Use.*Learning$", txt)
if (x):
   print("YES! We have a match!")
else:
   print("No match")

输出

YES! We have a match!

RegEx 函数

re 模块提供了几个函数,允许我们在字符串中搜索匹配项。

函数
说明
findall
返回包含所有匹配项的列表
search
如果在字符串中的任何位置找到匹配项,则返回 Match 对象
split
返回一个列表,其中字符串在每个数学运算处被拆分
sub
用字符串

元字符

RegEx 中的元字符是具有特殊含义的字符。

字符
说明
示例
[]
一组字符
“[a-m]”
\
表示特殊序列,也用于转义特殊字符
“\d”
.
除换行符之外的任何字符
“he..o”
^
以...开头
“^Hello”
$
以...结尾
“World$”
*
零次或多次
“aix*”
+
一次或多次出现次数
“aix+”
{>
精确指定出现次数
“a|{2}”
|
任意一个
“short|long”
()
捕获和分组
 

特殊序列

RegEx 中的特殊序列是 \ 后跟下面列出的字符之一,具有特殊含义 -

字符
说明
示例
\A
如果指定字符位于字符串的开头,则返回匹配项
“\APyt”
\b
如果指定字符位于单词的开头或结尾,则返回匹配项
r”\bPython” r”world\b”
\B
如果指定字符存在但不在单词的开头(或结尾),则返回匹配项
r”\BPython” r”World\B”
\d
如果字符串包含数字,则返回匹配项
“\d”
\D
如果字符串不包含数字,则返回匹配项
“\D”
\s
如果字符串包含空格,则返回匹配项
“\s”
\S
如果字符串不包含空格,则返回匹配项字符
“\S”
\w
如果字符串包含任何单词字符(字符可以是从 a 到 Z 的字母、从 0 到 9 的数字和下划线 _ 字符),则返回匹配项
“\w”
\W
如果字符串不包含任何单词字符,则返回匹配项
“\W”
\Z
如果指定的字符位于字符串末尾,则返回匹配项字符串
“world\Z”

集合

RegEx 中的集合是方括号 [] 内的一组字符,具有某些特殊含义。

集合
说明
[raj]
如果存在指定字符 (a、r 或 n) 之一,则返回匹配项
[a-r]
返回字母顺序介于 a 和 r 之间的任何小写字母的匹配项
[^raj]
返回除 r、a 和j
[0123]
返回匹配项,其中任意特定
[0-9]
返回匹配项,匹配 0 到 9 之间的任意数字
[0-3][0-8]
返回匹配项,匹配 00 到 38 之间的任意两位数
[a-zA-Z]
返回匹配项,匹配项按字母顺序排列,从 a 到 z 或从 A 到 Z 之间的任意字符
[+]
返回匹配项,匹配项为字符串

 

示例 - findall()

findall() 函数返回包含所有匹配项的列表。

#从文本中打印所有匹配项("in")的列表
import re
txt = "Use of python in Machine Learning"
x = re.findall("in", txt)
print(x)

输出

['in', 'in', 'in']

上面的输出显示列表按找到的顺序包含所有匹配项。但是,如果没有找到匹配项,则显示一个空列表。

只需更改上面程序中的以下行,文本或字符串中不存在的"pattern"。

x = re.findall("Hello", txt)

输出

[]

示例 - search() 函数

search() 函数搜索字符串,如果找到匹配项,则返回匹配对象。

但是,如果有多个匹配项,则只会返回匹配项的第一个出现项。

import re
txt = "Python is one of the most popular languages around the world"
searchObj = re.search("\s", txt)
print("第一个空白字符位于位置:", searchObj.start())

输出

第一个空白字符位于位置:6

但是,如果未找到匹配项,则返回 None。

示例 - split() 函数

RegEx 中的 split() 函数返回一个列表,其中字符串在每个匹配项处被拆分 -

# 在每个空白字符处拆分
import re
string = "Python is one of the most popular languages around the world"
searchObj = re.split("\s", string)
print(searchObj)

结果

['Python', 'is', 'one', 'of', 'the', 'most', 'popular', 'languages', 'around', 'the', 'world']

示例 - sub() 函数

RegEx 中的 sub() 函数用于将匹配项替换为您选择的文本。

#将字符串中的每个空格替换为 _:
import re
string = "Python is one of the most popular language around the world"
searchObj = re.sub("\s", "_", string)
print(searchObj)

结果

Python_is_one_of_the_most_popular_language_around_the_world

匹配对象

RegEx 中的匹配对象是包含有关搜索和结果的信息的对象。如果没有找到匹配项,则返回 None。

示例 - 搜索字符串并返回匹配对象。

import re
string = "Python is one of the most popular language around the world"
searchObj = re.search("on", string)
print(searchObj)

结果

<_sre.SRE_Match object; span=(4, 6), match='on'>

匹配对象具有用于检索有关搜索和结果的信息的属性和方法。

  • .span() –返回一个包含找到的匹配项的开始和结束位置的元组。

  • .string – 返回传递给函数的字符串。

  • .group() – 返回字符串中存在匹配项的部分。

示例 - 打印字符串中存在匹配项的部分。

#查找以大写字母"P"开头的任何单词:
import re
string = "Python is one of the most popular language around the world"
searchObj = re.search(r"\bP\w+", string)
print(searchObj)

结果

<_sre.SRE_Match object; span=(0, 6), match='Python'>

相关文章


有用资源