python re.sub的使用

python re.sub 使用起来很方便,写 python 代码常常都会用到。了解它的用法是很有必要的。

源代码中定义如下:

def sub(pattern, repl,string, count=0, flags=0):
    """Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the match object and must return
    a replacement string to be used."""
    return _compile(pattern, flags).sub(repl,string, count)
  • pattern,可以是一个字符串也可以是一个正则表达式,用于匹配要替换的字符,如果不写,字符串不做修改。
  • repl,表示要替换的字符串(即匹配到pattern后替换为repl),也可以是个函数。

    • 如果是一个字符串,反斜杠会被处理为转义字符。\g<1> 代表前面pattern里面第一个分组,可以简写为\1\g<0>代表前面pattern匹配到的所有字符串

    • repl如果是一个function,每一个被匹配到的字段串执行替换函数。if it is a callable, it's passed the match object and must return a replacement string to be used

  • string,表示要被处理(查找替换)的原始字符串;
  • count,是pattern被替换的最大次数,默认是0会替换所有。有时候可能只想替换一部分,可以用到count
  • flags,匹配模式

示例 1:

import re

a = re.sub(r'hello','i love you','hello world')
print(a)

执行结果:

i love you world

示例 2:

import re

a = re.sub(r'(\d+)','100','www.baidu.com is 3 yeas old!')
print(a) 

执行结果:

www.baidu.com is 100 yeas old! 

示例 3:

import re

a = re.sub(r'(\d+).*','\g<1> good tutorial website!','www.baidu.com is 3 yeas old!')
print(a)

执行结果:

www.baidu.com is 3 good tutorial website! 

示例 4:

import re

str1 ='<ol start="4" data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;" class="list-paddingleft-2">'
str2 ='<ol data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;color: black;" class="list-paddingleft-2">'

a = re.sub(r'<ol .*?(start=".*?"){0,1}.*?>','<ol \g<1>>', str1)
print(a)
b = re.sub(r'<ol .*?(start=".*?"){0,1}.*?>','<ol \g<1>>', str2)
print(b) 

执行结果:

<ol start="4">
<ol >  

示例 5:

import re
def replace_num(str):
    numDict ={'0':'〇','1':'一','2':'二','3':'三','4':'四','5':'五','6':'六','7':'七','8':'八','9':'九'}
    print(str.group(0))
    return numDict[str.group(0)]
my_str ='2021年11月01号'
a = re.sub(r'(\d)', replace_num, my_str)
print(a)#每次匹配一个数字,执行函数,获取替换后的值 

执行结果:

2
0
2
1
1
1
0
1
二〇二一年一一月〇一号

=========================================

import re
def replace_num(str):
    numDict ={'0':'〇','1':'一','2':'二','3':'三','4':'四','5':'五','6':'六','7':'七','8':'八','9':'九'}
    print(str.groups())
    return numDict[str.group(0)]
my_str ='2021年11月01号'
a = re.sub(r'(\d)', replace_num, my_str)
print(a)#每次匹配一个数字,执行函数,获取替换后的值 

执行结果:

('2',)
('0',)
('2',)
('1',)
('1',)
('1',)
('0',)
('1',)
二〇二一年一一月〇一号

 ============================================

import re
def replace_num(str):
    numDict ={'0':'〇','1':'一','2':'二','3':'三','4':'四','5':'五','6':'六','7':'七','8':'八','9':'九'}
    print(str)
    return numDict[str.group(0)]
my_str ='2021年11月01号'
a = re.sub(r'(\d)', replace_num, my_str)
print(a)#每次匹配一个数字,执行函数,获取替换后的值 

执行结果:

<re.Match object; span=(0, 1), match='2'>
<re.Match object; span=(1, 2), match='0'>
<re.Match object; span=(2, 3), match='2'>
<re.Match object; span=(3, 4), match='1'>
<re.Match object; span=(5, 6), match='1'>
<re.Match object; span=(6, 7), match='1'>
<re.Match object; span=(8, 9), match='0'>
<re.Match object; span=(9, 10), match='1'>
二〇二一年一一月〇一号

  

 

posted on 2021-11-01 19:43  朴素贝叶斯  阅读(5413)  评论(0编辑  收藏  举报

导航