python中正則表達式使用
文章目錄
- python中正則表達式使用
- 一、簡介
- 二、使用
- 2.1 常用規則
- 2.1.1 正則表達式字符串寫法
- 2.1.2 常用匹配規則
- 2.1.3 貪婪與非貪婪匹配
- 2.2 常用方法
- 2.2.1 編譯
- 2.2.2 匹配
- 2.2.3 查找
- 2.2.4 替換
- 2.2.5 切分
- 2.3 分組
- 2.3.1 分組使用
- 2.3.2 指定分組不捕獲
- 2.3.3 分組特殊規則
- 2.4 斷言
一、簡介
這里介紹python中的正則表達式使用,包含正則表達式常用規則、常用方法、貪婪與非貪婪匹配、分組、斷言等操作。
二、使用
這里預先定義待匹配字符串為:
#待匹配字符串
str
=
'make progress everyday ! 123456, and good aNd yes and haha AND 123 '
2.1 常用規則
2.1.1 正則表達式字符串寫法
正則表達式是一個字符串,在表達前添加 r 可以避免簡寫時對如 / 進行轉譯。如:
pat
=
re
.
compile
(
r
'\d+'
)
2.1.2 常用匹配規則
#符號
.:匹配除\n外的任意字符
[]:匹配中括號內指定字符
[^]:匹配除中括號內指定字符外的其他任意字符
():分組
#匹配簡寫
\d:匹配數字
\D:匹配非數字
\w:匹配數字、字母、下劃線
\W:匹配非數字、字母、下劃線
\s:匹配空白,空格和tab
\S:匹配非數字
\b:匹配數字
\B:匹配非數字
#匹配次數
*:匹配大于等于0次
+:匹配大于0次
?:匹配0或1次
{min,max}:匹配在min和max指定次數之間
#特殊規則
re.I 忽略大小寫,同re.IGNORECASE,或同分組中的(?i:正則表達式)。下面其他模塊類似
re.S 使.匹配包含換行符在內的所有字符
re.M 多行匹配,影響開頭和結束符,即: ^和$
re.X 為了增加可讀性,忽略空格和 # 后面的注釋
2.1.3 貪婪與非貪婪匹配
匹配默認是貪婪匹配(也就是盡量多的匹配字符串),正則后添加?即為非貪婪模式,如:.*? , \d+? ,
示例如:
# 非貪婪
obj
=
re
.
findall
(
'\d+?'
,
str
)
print
(
obj
)
# ['1', '2', '3', '4', '5', '6', '1', '2', '3']
# 貪婪
obj
=
re
.
findall
(
'\d+'
,
str
)
print
(
obj
)
# ['123456', '123']
2.2 常用方法
2.2.1 編譯
#re.compile 預編譯正則,可多次使用,編譯后可直接調用相關函數
pat
=
re
.
compile
(
r
'\d+'
)
print
(
pat
.
findall
(
str
)
)
#['123456', '123']
2.2.2 匹配
# re.match 從文本開頭匹配,如果一開始就失敗,則返回None
obj
=
re
.
match
(
'and'
,
str
)
print
(
obj
.
group
(
)
if
obj
else
'None'
)
#None
# re.fullmatch 全文匹配
obj
=
re
.
fullmatch
(
'.*?(\d+).*'
,
str
)
print
(
obj
.
groups
(
)
if
obj
else
'None'
)
#('123456',)
2.2.3 查找
# re.search 從全文匹配,直到找到一處匹配返回
obj
=
re
.
search
(
'and'
,
str
)
print
(
obj
.
group
(
)
if
obj
else
'None'
)
#and
# re.findall 查找,返回所有
obj
=
re
.
findall
(
'and'
,
str
,
re
.
I
)
print
(
obj
)
#['and', 'aNd', 'and', 'AND']
# re.finditer 查找,返回結果的迭代器
obj
=
re
.
finditer
(
'and'
,
str
)
print
(
list
(
map
(
lambda
m
:
m
.
group
(
)
,
obj
)
)
)
#['and', 'and']
2.2.4 替換
# re.sub 字符串替換,直接字符串替換
obj
=
re
.
sub
(
'and'
,
'*and*'
,
str
)
print
(
obj
)
# make progress everyday ! 123456, *and* good aNd yes *and* haha AND 1
# re.sub 字符串替換,直接字符串替換,同時指定替換的次數
obj
=
re
.
sub
(
'and'
,
'*and*'
,
str
,
1
)
print
(
obj
)
# make progress everyday ! 123456, *and* good aNd yes and haha AND 123
# # re.sub 字符串替換,直接字符串替換,忽略大小寫
obj
=
re
.
sub
(
'and'
,
'*and*'
,
str
,
flags
=
re
.
IGNORECASE
)
print
(
obj
)
# make progress everyday ! 123456, *and* good *and* yes *and* haha *and* 123
# re.sub 字符串替換,函數替換, 替換函數也可使用lambda表達式,lambda m: '--{}--'.format(m.group())
def
repl
(
m
)
:
return
'--{}--'
.
format
(
m
.
group
(
)
)
obj
=
re
.
sub
(
'and'
,
repl
,
str
)
print
(
obj
)
# make progress everyday ! 123456, --and-- good aNd yes --and-- haha AND 123
# re.sub 字符串替換,函數替換, 替換函數使用lambda表達式,
obj
=
re
.
sub
(
'and'
,
lambda
m
:
'--{}--'
.
format
(
m
.
group
(
)
)
,
str
)
print
(
obj
)
# make progress everyday ! 123456, --and-- good aNd yes --and-- haha AND 123
# re.subn 字符串替換,直接字符串替換,返回替換的字符串及替換的次數
obj
=
re
.
subn
(
'and'
,
'*and*'
,
str
,
flags
=
re
.
I
)
print
(
obj
)
# ('make progress everyday ! 123456, *and* good *and* yes *and* haha *and* 123 ', 4)
2.2.5 切分
# re.split 分割
obj
=
re
.
split
(
'and'
,
str
,
flags
=
re
.
I
)
print
(
obj
)
#['make progress everyday ! 123456, ', ' good ', ' yes ', ' haha ', ' 123 ']
2.3 分組
2.3.1 分組使用
使用()對指定表達式括起來即可分組。示例如下:
obj
=
re
.
match
(
'\D*(\d+)\D*(\d+)\D*'
,
str
)
# 源字符串
print
(
obj
.
group
(
)
)
#make progress everyday ! 123456, and good aNd yes and haha AND 123
# 匹配的字符串元組
print
(
obj
.
groups
(
)
)
(
'123456'
,
'123'
)
# 匹配字符串的最大位置索引
print
(
obj
.
lastindex
)
#2
print
(
obj
.
group
(
1
)
)
#123456
print
(
obj
.
group
(
2
)
)
#123
# 引用為實際分組字符串完全一樣,也就是引用的是文本,不是正則表達式
# 分組直接引用,格式:\分組位置 , 如:\1 , \2
obj
=
re
.
match
(
r
'.*(\d).*\1.*'
,
str
)
print
(
obj
.
groups
(
)
)
#('3',)
# 分組命名,格式:(?P<分組名>正則表達式)
obj
=
re
.
match
(
r
'\D*(?P
\d+)\D*(?P
\d+)\D*'
,
str
)
# 返回匹配串的key/val映射類型
print
(
obj
.
groupdict
(
)
)
# {'numOne': '123456', 'numTwo': '123'}
# 根據命名獲取匹配串映射類型
print
(
obj
.
group
(
'numOne'
)
)
# 123456
# 分組命名引用,格式:(?P=引用的分組名)
obj
=
re
.
match
(
r
'.*(?P
and).*(?P=numOne).*'
,
str
)
print
(
obj
.
groups
(
)
)
#('and',)
# 分組直接引用,格式:\分組位置 , 如:\1 , \2
obj
=
re
.
match
(
r
'.*(?P
and).*\1.*'
,
str
)
print
(
obj
.
groups
(
)
)
#('and',)
2.3.2 指定分組不捕獲
在分組()開頭添加 :? 表示該分組結果不捕獲,示例如下:
# 內部分組捕獲了
obj
=
re
.
findall
(
r
'((and)\s+\w+)'
,
str
)
print
(
obj
)
# [('and make', 'and'), ('and good', 'and'), ('and haha', 'and')]
# 內部分組沒有捕獲
obj
=
re
.
findall
(
r
'((?:and)\s+\w+)'
,
str
)
print
(
obj
)
# ['and make', 'and good', 'and haha']
2.3.3 分組特殊規則
添加特殊規則,在分組()開頭使用?加i(同re.I,大小寫忽略,其他標示類似), m, 或 x 標示標記,如(?i:正則表達式),
去掉特殊規則,在分組()開頭使用?-加i(同re.I,大小寫忽略,其他標示類似), m, 或 x 標示標記,如(-i:正則表達式),
示例如:
# 分組忽略大小寫
obj
=
re
.
findall
(
'(?i:and)'
,
str
)
print
(
obj
)
#['and', 'aNd', 'and', 'AND']
# 正則表達式整體忽略大小寫,但指定分組不忽略大小寫
obj
=
re
.
findall
(
'(?-i:and)'
,
str
,
re
.
I
)
print
(
obj
)
#['and', 'and']
2.4 斷言
斷言可以對匹配的字符串前后進行規定(對匹配的字符串前后再次添加條件限制)。
# 后向肯定斷言,格式:(?=正則表達式)
obj
=
re
.
findall
(
r
'(?P
\s*\w+\s*)(?=progress)'
,
str
)
print
(
obj
)
#['make ']
# 前向肯定斷言,格式:(?<=正則表達式)
obj
=
re
.
findall
(
r
'(?<=progress)(?P
\s*\w+\s*)'
,
str
)
print
(
obj
)
#[' everyday ']
# 后向否定斷言,格式:(?!正則表達式)
obj
=
re
.
findall
(
r
'(?P
\w+\s+)(?!progress)'
,
str
)
print
(
obj
)
#['progress ', 'everyday ', 'and ', 'good ', 'aNd ', 'yes ', 'and ', 'haha ', 'AND ', '123 ']
# 前向否定斷言,格式:(?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
