詳解Python中re.sub

【背景】
Python中的正則表達式方面的功能,很強大。
其中就包括re.sub,實現(xiàn)正則的替換。
功能很強大,所以導(dǎo)致用法稍微有點復(fù)雜。
所以當(dāng)遇到稍微復(fù)雜的用法時候,就容易犯錯。
所以此處,總結(jié)一下,在使用re.sub的時候,需要注意的一些事情。

解釋具體的注意事項之前,先把其具體的解釋貼出來:
re.sub
re.
sub
(
pattern, repl, string, count=0, flags=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is,\n
is converted to a single newline character,\r
is converted to a carriage return, and so forth. Unknown escapes such as\j
are left alone. Backreferences, such as\6
, are replaced with the substring matched by group 6 in the pattern. For example:

re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9])\s(\s):',... r'static PyObject\npy_\1(void)\n{',... 'def myfunc():')'static PyObject\npy_myfunc(void)\n{'
If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string. For example:
def dashrepl(matchobj):... if matchobj.group(0) == '-': return ' '... else: return '-'>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')'pro--gram files'>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)'Baked Beans & Spam'
The pattern may be a string or an RE object.
The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous match, sosub('x
', '-', 'abc')
returns'-a-b-c-'
.
In addition to character escapes and backreferences as described above,\g<name>
will use the substring matched by the group namedname
, as defined by the(?P<name>...)
syntax.\g<number>
uses the corresponding group number;\g<2>
is therefore equivalent to\2
, but isn’t ambiguous in a replacement such as\g<2>0
.\20
would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character'0'
. The backreference\g<0>
substitutes in the entire substring matched by the RE.
Changed in version 2.7: Added the optional flags argument.

re.sub的功能
re是regular expression的所寫,表示正則表達式
sub是substitute的所寫,表示替換;
re.sub是個正則表達式方面的函數(shù),用來實現(xiàn)通過正則表達式,實現(xiàn)比普通字符串的replace更加強大的替換功能;
舉個最簡單的例子:
如果輸入字符串是:
?
1

inputStr

"hello 111 world 111"

那么你可以通過
?
1

replacedStr

inputStr.replace(
"111"
,
"222"
)

去換成
"hello 222 world 222"

但是,如果輸入字符串是:
?
1

inputStr

"hello 123 world 456"

而你是想把123和456,都換成222
(以及其他還有更多的復(fù)雜的情況的時候),
那么就沒法直接通過字符串的replace達到這一目的了。
就需要借助于re.sub,通過正則表達式,來實現(xiàn)這種相對復(fù)雜的字符串的替換:
?
1

replacedStr

re.sub(
"\d+"
,
"222"
, inputStr)

當(dāng)然,實際情況中,會有比這個例子更加復(fù)雜的,其他各種特殊情況,就只能通過此re.sub去實現(xiàn)如此復(fù)雜的替換的功能了。
所以,re.sub的含義,作用,功能就是:
對于輸入的一個字符串,利用正則表達式(的強大的字符串處理功能),去實現(xiàn)(相對復(fù)雜的)字符串替換處理,然后返回被替換后的字符串
其中re.sub還支持各種參數(shù),比如count指定要替換的個數(shù)等等。
下面就是來詳細(xì)解釋其各個參數(shù)的含義。

re.sub的各個參數(shù)的詳細(xì)解釋
re.sub共有五個參數(shù)。
其中三個必選參數(shù):pattern, repl, string
兩個可選參數(shù):count, flags

第一個參數(shù):pattern
pattern,表示正則中的模式字符串,這個沒太多要解釋的。
需要知道的是:
反斜杠加數(shù)字(\N),則對應(yīng)著匹配的組(matched group)比如\6,表示匹配前面pattern中的第6個group
意味著,pattern中,前面肯定是存在對應(yīng)的,第6個group,然后你后面也才能去引用

比如,想要處理:
hello crifan, nihao crifan

且此處的,前后的crifan,肯定是一樣的。
而想要把整個這樣的字符串,換成crifanli
則就可以這樣的re.sub實現(xiàn)替換:
?
1
2
3

inputStr

"hello crifan, nihao crifan"
;

replacedStr

re.sub(r
"hello (\w+), nihao \1"
,
"crifanli"
, inputStr);

print
"replacedStr="
,replacedStr;

crifanli

第二個參數(shù):repl
repl,就是replacement,被替換,的字符串的意思。
repl可以是字符串,也可以是函數(shù)。

repl是字符串

如果repl是字符串的話,其中的任何反斜杠轉(zhuǎn)義字符,都會被處理的。
即:
\n:會被處理為對應(yīng)的換行符;
\r:會被處理為回車符;
其他不能識別的轉(zhuǎn)移字符,則只是被識別為普通的字符:比如\j,會被處理為j這個字母本身;

反斜杠加g以及中括號內(nèi)一個名字,即:\g<name>,對應(yīng)著命了名的組,named group

接著上面的舉例:
想要把對應(yīng)的:
hello crifan, nihao crifan

中的crifan提取出來,只剩:
crifan

就可以寫成:
?
1
2
3

inputStr

"hello crifan, nihao crifan"
;

replacedStr

re.sub(r
"hello (\w+), nihao \1"
,
"\g<1>"
, inputStr);

print
"replacedStr="
,replacedStr;

crifan

對應(yīng)的帶命名的組(named group)的版本是:
?
1
2
3

inputStr

"hello crifan, nihao crifan"
;

replacedStr

re.sub(r
"hello (?P<name>\w+), nihao (?P=name)"
,
"\g<name>"
, inputStr);

print
"replacedStr="
,replacedStr;

crifan

repl是函數(shù)

舉例說明:
比如輸入內(nèi)容是:
hello 123 world 456

想要把其中的數(shù)字部分,都加上111,變成:
hello 234 world 567

那么就可以寫成:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

!/usr/bin/python

-- coding: utf-8 --

"""

Function:

【整理】詳解Python中re.sub

http://www.crifan.com/python_re_sub_detailed_introduction

Version: 2013-05-02

Author: Crifan

Contact: admin (at) crifan.com

"""

import
re;

def
pythonReSubDemo():

"""

demo Pyton re.sub

"""

inputStr

"hello 123 world 456"
;

def
_add111(matched):

intStr

matched.group(
"number"
);

123

intValue

int
(intStr);

addedValue

intValue

111
;

234

addedValueStr

str
(addedValue);

return
addedValueStr;

replacedStr

re.sub(
"(?P<number>\d+)"
, _add111, inputStr);

print
"replacedStr="
,replacedStr;

hello 234 world 567

###############################################################################

if
name
=
=
"main"
:

pythonReSubDemo();

第三個參數(shù):string
string,即表示要被處理,要被替換的那個string字符串。
沒什么特殊要說明。

第四個參數(shù):count
舉例說明:
繼續(xù)之前的例子,假如對于匹配到的內(nèi)容,只處理其中一部分。
比如對于:
hello 123 world 456 nihao 789

只是像要處理前面兩個數(shù)字:123,456,分別給他們加111,而不處理789,
那么就可以寫成:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

!/usr/bin/python

-- coding: utf-8 --

"""

Function:

【整理】詳解Python中re.sub

http://www.crifan.com/python_re_sub_detailed_introduction

Version: 2013-05-02

Author: Crifan

Contact: admin (at) crifan.com

"""

import
re;

def
pythonReSubDemo():

"""

demo Pyton re.sub

"""

inputStr

"hello 123 world 456 nihao 789"
;

def
_add111(matched):

intStr

matched.group(
"number"
);

123

intValue

int
(intStr);

addedValue

intValue

111
;

234

addedValueStr

str
(addedValue);

return
addedValueStr;

replacedStr

re.sub(
"(?P<number>\d+)"
, _add111, inputStr,
2
);

print
"replacedStr="
,replacedStr;

hello 234 world 567 nihao 789

###############################################################################

if
name
=
=
"main"
:

pythonReSubDemo();

第五個參數(shù):flags

關(guān)于re.sub的注意事項
然后再來整理一些,關(guān)于re.sub的注意事項,常見的問題及解決辦法:

要注意,被替換的字符串,即參數(shù)repl,是普通的字符串,不是pattern
注意到,語法是:
?
1

re.sub(pattern, repl, string, count

0
, flags
=
0
)

即,對應(yīng)的第二個參數(shù)是repl。
需要你指定對應(yīng)的r前綴,才是pattern:
r"xxxx"

不要誤把第四個參數(shù)flag的值,傳遞到第三個參數(shù)count中了
否則就會出現(xiàn)我這里:
【已解決】Python中,(1)re.compile后再sub可以工作,但re.sub不工作,或者是(2)re.search后replace工作,但直接re.sub以及re.compile后再re.sub都不工作
遇到的問題:
當(dāng)傳遞第三個參數(shù),原以為是flag的值是,
結(jié)果實際上是count的值
所以導(dǎo)致re.sub不功能,
所以要參數(shù)指定清楚了:
?
1

replacedStr

re.sub(replacePattern, orignialStr, replacedPartStr, flags

re.I);

can omit count parameter

或:
?
1

replacedStr

re.sub(replacePattern, orignialStr, replacedPartStr,
1
, re.I);

must designate count parameter

才可以正常工作。https://www.crifan.com/python_re_sub_detailed_introduction/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容