python字符串替换之re.sub()

re.sub(patternreplstringcount=0flags=0)

pattern可以是一个字符串也可以是一个正则,用于匹配要替换的字符,如果不写,字符串不做修改。\1 代表第一个分组

repl是将会被替换的值,repl可以是字符串也可以是一个方法。如果是一个字符串,反斜杠会被处理为逃逸字符,如\n会被替换为换行,等等。repl如果是一个function,每一个被匹配到的字段串执行替换函数。

\g<1> 代表前面pattern里面第一个分组,可以简写为\1,\g<0>代表前面pattern匹配到的所有字符串。

count是pattern被替换的最大次数,默认是0会替换所有。有时候可能只想替换一部分,可以用到count

实例1:

a = re.sub(r‘hello‘, ‘i love the‘, ‘hello world‘)
print(a)‘i love the world‘   #hello world里面的hello被 i love the替换

实例2:

>>> a = re.sub(r‘(\d+)‘, ‘hello‘, ‘my numer is 400 and door num is 200‘)
>>> a
‘my numer is hello and door num is hello‘ #数字400 和 200 被hello替换

实例3:

a = re.sub(r‘hello (\w+), nihao \1‘, r‘emma‘,‘hello sherry, nihao sherry‘)
>>> a
‘emma‘  #\1代表第一个分组的值即sherry,因为有两个sherry,所以用\1可以指代第二个,这样整个字符串被emma替换

示例4:

>>> a = re.sub(‘(\d{4})-(\d{2})-(\d{2})‘, r‘\2-\3-\1‘, ‘2018-06-07‘)
>>> a
‘06-07-2018‘
>>> a = re.sub(‘(\d{4})-(\d{2})-(\d{2})‘, r‘\g<2>-\g<3>-\g<1>‘, ‘2018-06-07‘)
>>> a
‘06-07-2018‘  #\2 和 \g<2> 指代的的都是前面的第二个分组

示例5:

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

re.subn(patternreplstringcount=0flags=0)

和sub()函数一样,只是返回的是一个tuple,替换后的字符串和替换的个数  

 

 

原文地址:https://www.cnblogs.com/guoxueyuan/p/9151678.html

时间: 2024-08-03 07:23:20

python字符串替换之re.sub()的相关文章

python字符串替换的2种有效方法

python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'我把a字符串里的word替换为python1用字符串本身的replace方法a.replace('word','python')输出的结果是hello python 2用正则表达式来完成替换:import restrinfo = re.compile('word')b = strinfo.sub('python',a)print b输出的结果也是hell

python字符串替换的2种方法

python字符串替换可以用两种方法实现: 1.用字符串本身的方法 2.用正则来替换字符串 下面用个例子来实验: a = 'hello word' 我把a字符串里的word替换为python 1.用字符串本身的replace方法 a.replace('word' , 'python') 输出结果是hello  python 2.用正则表达式来完成替换: import  re strinfo = re . compile('word') b = strinfo.sub('python',a) pr

python 字符串替换

字符串替换可以用内置的方法和正则表达式完成. 1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b 2用正则表达式来完成替换: import re a = 'hello word' strinfo = re.compile('word') b = strinfo.sub('python',a) print b 3 单字符多次替换 import reastr = 'AGTCTTAGGC'charmap

python 字符串替换功能 string.replace()可以用正则表达式,更优雅

说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变得优雅简洁: 备注:上图中的base_info 是 pandas 里的 dataframe 数据结构,可以用上述方法使用 string 的 replace 方法. 原文地址:https://www.cnblogs.com/jjliu/p/11514226.html

python 字符串替换、正则查找替换

import re if __name__ == "__main__": url = " \n deded<a href = "">这是第一个链接</a><a href = "">这是第二个链接</a> \n " # 去除\n one = url.replace("\n", "") # 去掉两端空格 two = one.strip() #

Python字符串拼接、截取及替换方法总结

字符串拼接: 用字符串的join方法: a = ['a','b','c','d'] content = '' content = ''.join(a) print content content的结果:'abcd' 用字符串的替换占位符替换: a = ['a','b','c','d'] content = '' content = '%s%s%s%s' % tuple(a)  print content content的结果是:'abcd' 字符串截取: python的字串列表有2种取值顺序 1

Python 正则表达式替换特定字符为标志的字符串

Requirement: [ { "code": "AF", "value": 53, "name": "Afghanistan" }, { "code": "AL", "value": 117, "name": "Albania" }, { "code": "DZ",

python字符串(大小写、判断、查找、分割、拼接、裁剪、替换、格式化)

一.通用操作 1.Python len() 方法返回对象(字符.列表.元组等)长度或项目个数. 语法 len()方法语法: len( q ) 参数 q -- 对象. 返回值 返回对象长度. 实例 以下实例展示了 len() 的使用方法: >>>str = "runoob" >>> len(str) # 字符串长度 6 >>> l = [1,2,3,4,5] >>> len(l) # 列表元素个数 5 2.pytho

python文件操作--字符串替换

如把test.txt文件的 所有 AAA 字符串 替换成 aaaaa 1 with open('test.txt','+r') as f: 2 t = f.read() 3 t = d.replace('AAA', 'aaaaaa') 4 #读写偏移位置移到最开始处 5 f.seek(0, 0) 6 f.write(t)