带有示例的Python正则表达式?

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

正则表达式或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!

正则表达式功能

re模块提供了几个功能,使我们可以搜索字符串以查找匹配项。

功能
描述
找到所有
返回包含所有匹配项的列表
搜索
如果在字符串中的任何位置找到匹配项,则返回Match对象
分裂
返回一个列表,该字符串在每个数学运算中均已拆分

用字符串替换一个或多个匹配项

元字符

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

性格
描述

[]
一组字符
“[上午]”
\
发出特殊序列的信号,也用于转义特殊字符
“ \ d”

除换行符外的任何字符
“他..o”
^
以。。开始
“ ^你好”
$
以。。结束
“世界$”
*
零次或更多次
“ aix *”
+
一个或多个事件
“ aix +”
{}
确切指定的发生次数
“ a | {2}”
|
两者任一
“短|长”
()
捕获并分组
 

特殊序列

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

字符
描述

\一种
如果指定的字符在字符串的开头,则返回匹配项
“ \ 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
如果指定的字符位于字符串的末尾,则返回匹配项
“世界\ Z”

套装

RegEx中的set是在一对方括号[]中的一组字符,它们具有某些特殊含义。


描述
[raj]
如果存在指定的字符(a,r或n)之一,则返回匹配项
[ar]
返回任何小写字母的匹配项,按字母顺序在a和r之间
[^ raj]
返回除r,a和j以外的任何字符的匹配项
[0123]
返回任何spe匹配项
[0-9]
返回0到9之间的任何数字的匹配项
[0-3] [0-8]
返回00到38之间的任何两位数字的匹配项
[a-zA-Z]
返回字母从a到z或A到Z的任何字符的匹配项
[+]
返回字符串中任何+字符的匹配项

 

范例- findall()

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

#Print a list of all matches (“in”) from a text
import re
txt = "Use of python in Machine Learning"
x = re.findall("in", txt)
print(x)

输出结果

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

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

只需在上面的程序中更改以下行,即“模式”,该行就不在文本或字符串中。

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("The first white-space character is located in position: ", searchObj.start())

输出结果

The first white-space character is located in position: 6

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

示例-split()函数

split()RegEx中的函数返回一个列表,该列表在每次匹配时均已将字符串分割开-

# Split at each white-space character
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()函数

sub()RegEx中的功能是将匹配项替换为您选择的文本。

#Replace every white-space in the string with _:
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中的match对象是包含有关搜索和结果信息的对象。在未找到匹配项的情况下,不返回任何值。

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

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() –返回字符串中匹配的部分。

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

#Looks for any words that starts with the an upper case “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'>