0
点赞
收藏
分享

微信扫一扫

python re(十一)


搜索和替换

其他常见的用途就是找到所有模式匹配的字符串并用不同的字符串来替换它们。sub() 方法提供一个替换值,可以是字符串或一个函数,和一个要被处理的字符串。

sub(replacement, string[, count  =  0])


返回的字符串是在字符串中用 RE 最左边不重复的匹配来替换。如果模式没有发现,字符将被没有改变地返回。


可选参数 count 是模式匹配後替换的最大次数;count 必须是非负整数。缺省值是 0 表示替换所有的匹配。


这里有个使用 sub() 方法的简单例子。它用单词 "colour" 替换颜色名。

# !python

>>> p = re.compile( ' (blue|white|red) ' )

>>> p.sub( ' colour ' , ' blue socks and red shoes ' )

' colour socks and colour shoes '

>>> p.sub( ' colour ' , ' blue socks and red shoes ' , count = 1 )

' colour socks and red shoes '



subn() 方法作用一样,但返回的是包含新字符串和替换执行次数的两元组。

# !python

>>> p = re.compile( ' (blue|white|red) ' )

>>> p.subn( ' colour ' , ' blue socks and red shoes ' )

( ' colour socks and colour shoes ' , 2 )

>>> p.subn( ' colour ' , ' no colours at all ' )

( ' no colours at all ' , 0)


空匹配只有在它们没有紧挨着前一个匹配时才会被替换掉。

# !python

>>> p = re.compile( ' x* ' )

>>> p.sub( ' - ' , ' abxd ' )

' -a-b-d- '


如果替换的是一个字符串,任何在其中的反斜杠都会被处理。""n" 将会被转换成一个换行符,""r"转换成回车等等。未知的转义如 ""j" 则保持原样。逆向引用,如 ""6",被 RE 中相应的组匹配而被子串替换。这使你可以在替换後的字符串中插入原始文本的一部分。


这个例子匹配被 "{" 和 "}" 括起来的单词 "section",并将 "section" 替换成 "subsection"。


# !python

>>> p = re.compile( ' section{ ( [^}]* ) } ' , re.VERBOSE)

>>> p.sub(r ' subsection{"1} ' , ' section{First} section{second} ' )

' subsection{First} subsection{second} '

还可以指定用 (?P<name>...) 语法定义的命名组。""g<name>" 将通过组名 "name" 用子串来匹配,并且 ""g<number>" 使用相应的组号。所以 ""g<2>" 等于 ""2",但能在替换字符串里含义不清,如 ""g<2>0"。(""20" 被解释成对组 20 的引用,而不是对後面跟着一个字母 "0" 的组 2 的引用。)


# !python

>>> p = re.compile( ' section{ (?P<name> [^}]* ) } ' , re.VERBOSE)

>>> p.sub(r ' subsection{"1} ' , ' section{First} ' )

' subsection{First} '

>>> p.sub(r ' subsection{"g<1>} ' , ' section{First} ' )

' subsection{First} '

>>> p.sub(r ' subsection{"g<name>} ' , ' section{First} ' )

' subsection{First} '

替换也可以是一个甚至给你更多控制的函数。如果替换是个函数,该函数将会被模式中每一个不重复的匹配所调用。在每个调用时,函数被作为 `MatchObject` 的匹配函属,并可以使用这个信息去计算预期的字符串并返回它。


在下面的例子里,替换函数将十进制翻译成十六进制:


# !python

>>> def hexrepl( match ):

" Return the hex string for a decimal number "

value = int( match.group() )

return hex(value)



>>> p = re.compile(r ' "d+ ' )

>>> p.sub(hexrepl, ' Call 65490 for printing, 49152 for user code. ' )

' Call 0xffd2 for printing, 0xc000 for user code. '

当使用模块级的 re.sub() 函数时,模式作为第一个参数。模式也许是一个字符串或一个 `RegexObject`;如果你需要指定正则表达式标志,你必须要么使用 `RegexObject` 做第一个参数,或用使用模式内嵌修正器,如 sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'。


举报

相关推荐

0 条评论