Python正则表达式
RegEx或正则表达式是形成搜索模式的一系列字符。
RegEx可用于检查字符串是否包含指定的搜索模式。
正则表达式模块
Python有一个名为的内置程序包re
,可用于处理正则表达式。
导入re
模块:
import re
Python中的RegEx
导入re
模块后,可以开始使用正则表达式:
例
搜索字符串以查看它是否以“ The”开头并以“ Spain”结尾:
import
re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
自己尝试»
正则表达式功能
该re
模块提供了一组函数,使我们可以在字符串中搜索匹配项:
Function | Description |
---|---|
findall | Returns a list containing all matches |
search | Returns a Match object if there is a match anywhere in the string |
split | Returns a list where the string has been split at each match |
sub | Replaces one or many matches with a string |
元字符
元字符是具有特殊含义的字符:
Character | Description | Example | |
---|---|---|---|
[] | A set of characters | "[a-m]" | |
\ | Signals a special sequence (can also be used to escape special characters) | "\d" | |
. | Any character (except newline character) | "he..o" | |
^ | Starts with | "^hello" | |
$ | Ends with | "world$" | |
* | Zero or more occurrences | "aix*" | |
+ | One or more occurrences | "aix+" | |
{} | Exactly the specified number of occurrences | "al{2}" | |
| | Either or | "falls|stays" | |
() | Capture and group |
特殊序列
\
以下列表中的字符之一是一个特殊的序列,其后有特殊含义:
Character | Description | Example |
---|---|---|
\A | Returns a match if the specified characters are at the beginning of the string | "\AThe" |
\b | Returns a match where the specified characters are at the beginning or at the
end of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\bain" r"ain\b" |
\B | Returns a match where the specified characters are present, but NOT at the beginning
(or at
the end) of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\Bain" r"ain\B" |
\d | Returns a match where the string contains digits (numbers from 0-9) | "\d" |
\D | Returns a match where the string DOES NOT contain digits | "\D" |
\s | Returns a match where the string contains a white space character | "\s" |
\S | Returns a match where the string DOES NOT contain a white space character | "\S" |
\w | Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) | "\w" |
\W | Returns a match where the string DOES NOT contain any word characters | "\W" |
\Z | Returns a match if the specified characters are at the end of the string | "Spain\Z" |
套装
集合是在一对方括号内的一组字符
[]
,具有特殊含义:
Set | Description |
---|---|
[arn] | Returns a match where one of the specified characters (a ,
r , or n ) are
present |
[a-n] | Returns a match for any lower case character, alphabetically between
a and n |
[^arn] | Returns a match for any character EXCEPT a ,
r , and n |
[0123] | Returns a match where any of the specified digits (0 ,
1 , 2 , or
3 ) are
present |
[0-9] | Returns a match for any digit between
0 and 9 |
[0-5][0-9] | Returns a match for any two-digit numbers from 00 and
59 |
[a-zA-Z] | Returns a match for any character alphabetically between
a and z , lower case OR upper case |
[+] | In sets, + , * ,
. , | ,
() , $ ,{}
has no special meaning, so [+] means: return a match for any
+ character in the string |
findall()函数
该findall()
函数返回包含所有匹配项的列表。
列表按找到顺序包含匹配项。
如果找不到匹配项,则返回一个空列表:
例
如果找不到匹配项,则返回一个空列表:
import re
txt = "The rain in Spain"
x = re.findall("Portugal",
txt)
print(x)
自己尝试»
search()函数
该search()
函数在字符串中搜索匹配项,如果存在匹配项,则返回Match对象。
如果有多个匹配项,则仅返回匹配项的第一个匹配项:
例
搜索字符串中的第一个空格字符:
import re
txt = "The rain in Spain"
x = re.search("\s",
txt)
print("The first white-space character is located in
position:", x.start())
自己尝试»
如果未找到匹配项,None
则返回值:
split()函数
该split()
函数返回一个列表,其中每个匹配项都已将字符串分割开:
您可以通过指定maxsplit
参数来控制出现次数
:
sub()函数
该sub()
函数将匹配项替换为您选择的文本:
您可以通过指定count
参数来控制替换次数
:
匹配对象
匹配对象是包含有关搜索和结果信息的对象。
注意:如果没有匹配项,None
则将返回值,而不是匹配对象。
例
进行搜索以返回匹配对象:
import re
txt = "The rain in Spain"
x = re.search("ai",
txt)
print(x) #this will print an object
自己尝试»
Match对象具有用于检索有关搜索信息和结果的属性和方法:
.span()
返回一个元组,其中包含比赛的开始和结束位置。
.string
返回传递给函数
.group()
的字符串,返回匹配的字符串部分
例
打印第一个匹配项的位置(开始和结束位置)。
正则表达式查找以大写字母“ S”开头的所有单词:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
自己尝试»
例
打印传递给函数的字符串:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
自己尝试»
例
打印匹配的字符串部分。
正则表达式查找以大写字母“ S”开头的所有单词:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())
自己尝试»
注意:如果没有匹配项,None
则将返回值,而不是匹配对象。