📜  Python表达式

📅  最后修改于: 2020-09-19 14:58:37             🧑  作者: Mango

在本教程中,您将学习正则表达式(RegEx),并使用Python的re模块与RegEx一起使用(在示例的帮助下)。

gular PRESSION(正则表达式)是字符序列,它定义一个搜索模式。例如,

^a...s$

上面的代码定义了RegEx模式。模式是: a开头并以s结尾的任何五个字母字符串

使用RegEx定义的模式可用于与字符串匹配。

Expression String Matched?
^a...s$ abs No match
alias Match
abyss Match
Alias No match
An abacus No match

Python有一个名为re的模块可与RegEx一起使用。这是一个例子:

import re

pattern = '^a...s$'
test_string = 'abyss'
result = re.match(pattern, test_string)

if result:
  print("Search successful.")
else:
  print("Search unsuccessful.")

在这里,我们使用re.match() 函数在test_string搜索pattern 。如果搜索成功,该方法将返回一个匹配对象。如果不是,则返回None

re模块中定义了其他一些功能,可与RegEx一起使用。在探讨之前,让我们学习正则表达式本身。

如果您已经了解RegEx的基础知识,请跳至Python RegEx。

使用正则表达式指定模式

为了指定正则表达式,使用了元字符。在上面的示例中, ^$是元字符。

元字符

元字符是RegEx引擎以特殊方式解释的字符。以下是元字符列表:

[] ^ $ * + {} () \ |

[] -方括号

方括号指定您要匹配的一组字符。

ExpressionStringMatched?
[abc]a1 match
ac2 matches
Hey JudeNo match
abc de ca5 matches

在这里,如果您要匹配的字符串包含abc中的任何a ,则[abc]将匹配。

您还可以使用-在方括号内指定字符范围。

  1. [ae][abcde]相同。
  2. [1-4][1234]相同。
  3. [0-39][01239]相同。

您可以通过在方括号的开头使用插入号^符号来补充(反转)字符集。

  1. [^abc]表示除abc之外a任何字符。
  2. [^0-9]表示任何非数字字符。

. - 期间

句点匹配任何单个字符(换行符'\n'除外)。

ExpressionStringMatched?
..aNo match
ac1 match
acd1 match
acde2 matches (contains 4 characters)

^ - 插入符号

插入符号^用于检查字符串是否某个特定字符开头

ExpressionStringMatched?
^aa1 match
abc1 match
bacNo match
^ababc1 match
acbNo match (starts with a but not followed by b)

$ - 美元

美元符号$用于检查字符串是否以某个字符结尾

ExpressionStringMatched?
a$a1 match
formula1 match
cabNo match

* -

星号*匹配零个或多个剩余的模式。

ExpressionStringMatched?
ma*nmn1 match
man1 match
maaan1 match
mainNo match (a is not followed by n)
woman1 match

+ -

加号+匹配出现在其上的一个或多个模式。

ExpressionStringMatched?
ma+nmnNo match (no a character)
man1 match
maaan1 match
mainNo match (a is not followed by n)
woman1 match

? - 问号

问号符号?匹配零或一出现的模式。

ExpressionStringMatched?
ma?nmn1 match
man1 match
maaanNo match (more than one a character)
mainNo match (a is not followed by n)
woman1 match

{} - 大括号

考虑以下代码: {n,m} 。这意味着至少要保留n样式,最多m重复样式。

ExpressionStringMatched?
a{2,3}abc datNo match
abc daat1 match (at daat)
aabc daaat2 matches (at aabc and daaat)
aabc daaaat2 matches (at aabc and daaaat)

让我们再尝试一个示例。此RegEx [0-9]{2, 4}匹配至少2位但不超过4位

ExpressionStringMatched?
[0-9]{2,4}ab123csde1 match (match at ab123csde)
12 and 3456733 matches (12, 3456, 73)
1 and 2No match

| - 交替

竖条|用于交替( or 运算符)。

ExpressionStringMatched?
a|bcdeNo match
ade1 match (match at ade)
acdbea3 matches (at acdbea)

此处, a|b匹配包含ab任何字符串

() -

括号()用于对子模式进行分组。例如, (a|b|c)xz匹配任何匹配abc 字符串 ,后跟xz

ExpressionStringMatched?
(a|b|c)xzab xzNo match
abxz1 match (match at abxz)
axz cabxz2 matches (at axzbc cabxz)

\ - 反斜杠

反冲\用于转义包括所有元字符在内的各种字符。例如,

\$a如果字符串包含$后跟a $则匹配。此处,RegEx引擎不会以特殊方式解释$

如果不确定某个字符是否具有特殊含义,可以在其前面加上\ 。这样可以确保不对字符进行特殊处理。

特殊序列

特殊序列使常用模式更易于编写。以下是特殊序列的列表:

\A如果指定的字符位于字符串的开头,则匹配。

ExpressionStringMatched?
\Athethe sunMatch
In the sunNo match

\b如果指定字符在单词的开头或结尾,则匹配。

ExpressionStringMatched?
\bfoofootballMatch
a footballMatch
afootballNo match
foo\bthe fooMatch
the afoo testMatch
the afootestNo match

\B\b相反。如果指定的字符不在单词的开头或结尾,则匹配。

ExpressionStringMatched?
\BfoofootballNo match
a footballNo match
afootballMatch
foo\Bthe fooNo match
the afoo testNo match
the afootestMatch

\d匹配任何十进制数字。相当于[0-9]

ExpressionStringMatched?
\d12abc33 matches (at 12abc3)
PythonNo match

\D匹配任何非十进制数字。相当于[^0-9]

ExpressionStringMatched?
\D1ab34"503 matches (at 1ab34"50)
1345No match

\s匹配字符串包含任何空格字符的地方。等效于[ \t\n\r\f\v]

ExpressionStringMatched?
\sPython RegEx1 match
PythonRegExNo match

\S匹配字符串包含任何非空白字符的地方。等效于[^ \t\n\r\f\v]

ExpressionStringMatched?
\Sa b2 matches (at a b)
   No match

\w匹配任何字母数字字符(数字和字母)。等效于[a-zA-Z0-9_] 。顺便说一下,下划线_也被认为是字母数字字符。

ExpressionStringMatched?
\w12&": ;c 3 matches (at 12&": ;c)
%"> !No match

\W匹配任何非字母数字字符。等效于[^a-zA-Z0-9_]

ExpressionStringMatched?
\W1a2%c1 match (at 1a2%c)
PythonNo match

\Z如果指定的字符位于字符串的末尾,则匹配。

ExpressionStringMatched?
Python\ZI like Python1 match
I like Python ProgrammingNo match
Python is fun.No match

提示:要构建和测试正则表达式,可以使用RegEx测试器工具,例如regex101。该工具不仅可以帮助您创建正则表达式,而且还可以帮助您学习它。

现在,您了解了RegEx的基础知识,让我们讨论如何在Python代码中使用RegEx。

Python表达式

Python有一个名为re的模块,可用于正则表达式。要使用它,我们需要导入模块。

import re

该模块定义了一些可与RegEx一起使用的函数和常量。

re.findall()

re.findall()方法返回包含所有匹配项的字符串列表。

示例1:re.findall()

# Program to extract numbers from a string

import re

string = 'hello 12 hi 89. Howdy 34'
pattern = '\d+'

result = re.findall(pattern, string) 
print(result)

# Output: ['12', '89', '34']

如果找不到该模式,则re.findall()返回一个空列表。

re.split()

re.split方法在存在匹配项的情况下拆分字符串 ,并返回发生拆分的字符串列表。

示例2:re.split()

import re

string = 'Twelve:12 Eighty nine:89.'
pattern = '\d+'

result = re.split(pattern, string) 
print(result)

# Output: ['Twelve:', ' Eighty nine:', '.']

如果找不到该模式,则re.split()返回一个包含原始字符串的列表。

您可以将maxsplit参数传递给re.split()方法。这是将要发生的最大拆分次数。

import re

string = 'Twelve:12 Eighty nine:89 Nine:9.'
pattern = '\d+'

# maxsplit = 1
# split only at the first occurrence
result = re.split(pattern, string, 1) 
print(result)

# Output: ['Twelve:', ' Eighty nine:89 Nine:9.']

顺便说一句, maxsplit的默认值为0;表示所有可能的分裂。

re.sub()

re.sub()的语法为:

re.sub(pattern, replace, string)

该方法返回一个字符串 ,其中匹配的匹配项被replace变量的内容replace

示例3:re.sub()

# Program to remove all whitespaces
import re

# multiline string
string = 'abc 12\
de 23 \n f45 6'

# matches all whitespace characters
pattern = '\s+'

# empty string
replace = ''

new_string = re.sub(pattern, replace, string) 
print(new_string)

# Output: abc12de23f456

如果找不到该模式,则re.sub()返回原始字符串。

您可以将count作为第四个参数传递给re.sub()方法。如果省略,则结果为0。这将替换所有出现的事件。

import re

# multiline string
string = 'abc 12\
de 23 \n f45 6'

# matches all whitespace characters
pattern = '\s+'
replace = ''

new_string = re.sub(r'\s+', replace, string, 1) 
print(new_string)

# Output:
# abc12de 23
# f45 6

re.subn()

re.subn()re.sub()类似,期望它返回一个包含2个项目的元组,其中包含新字符串和进行替换的次数。

示例4:re.subn()

# Program to remove all whitespaces
import re

# multiline string
string = 'abc 12\
de 23 \n f45 6'

# matches all whitespace characters
pattern = '\s+'

# empty string
replace = ''

new_string = re.subn(pattern, replace, string) 
print(new_string)

# Output: ('abc12de23f456', 4)

研究()

re.search()方法采用两个参数:模式和字符串。该方法查找RegEx模式与字符串匹配的第一个位置。

如果搜索成功,则re.search()返回一个match对象。如果不是,则返回None

match = re.search(pattern, str)

示例5:re.search()

import re

string = "Python is fun"

# check if 'Python' is at the beginning
match = re.search('\APython', string)

if match:
  print("pattern found inside the string")
else:
  print("pattern not found")  

# Output: pattern found inside the string

在这里, match包含一个match对象。

匹配对象

您可以使用dir() 函数获取匹配对象的方法和属性。

匹配对象的一些常用方法和属性是:

match.group()

group()方法返回字符串中匹配的部分。

示例6:匹配对象

import re

string = '39801 356, 2102 1111'

# Three digit number followed by space followed by two digit number
pattern = '(\d{3}) (\d{2})'

# match variable contains a Match object.
match = re.search(pattern, string) 

if match:
  print(match.group())
else:
  print("pattern not found")

# Output: 801 35

在这里, match变量包含一个match对象。

我们的模式(\d{3}) (\d{2})有两个子组(\d{3})(\d{2}) 。您可以获取这些带括号的子组的字符串的一部分。这是如何做:

>>> match.group(1)
'801'

>>> match.group(2)
'35'
>>> match.group(1, 2)
('801', '35')

>>> match.groups()
('801', '35')

match.start(),match.end()和match.span()

start() 函数返回匹配的子字符串的开头的索引。同样, end()返回匹配的子字符串的结束索引。

>>> match.start()
2
>>> match.end()
8

span() 函数返回一个包含匹配部分的开始和结束索引的元组。

>>> match.span()
(2, 8)

match.re和match。 字符串

匹配对象的re属性返回一个正则表达式对象。同样, string属性返回传递的字符串。

>>> match.re
re.compile('(\\d{3}) (\\d{2})')

>>> match.string
'39801 356, 2102 1111'

我们已经介绍了re模块中定义的所有常用方法。如果您想了解更多,请访问Python 3 re模块。

在RegEx之前使用r前缀

在正则表达式前使用rR前缀时,表示原始字符串。例如, '\n'是换行,而r'\n'表示两个字符:反斜杠\后跟n

反冲\用于转义包括所有元字符在内的各种字符。但是,使用r前缀会使\视为普通字符。

示例7:使用r前缀的原始字符串

import re

string = '\n and \r are escape sequences.'

result = re.findall(r'[\n\r]', string) 
print(result)

# Output: ['\n', '\r']