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,替换后的字符串和替换的个数  

 

 

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

  1. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  2. python 字符串替换

    字符串替换可以用内置的方法和正则表达式完成.1用字符串本身的replace方法: a = 'hello word'b = a.replace('word','python')print b 2用正则表 ...

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

    python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'我把a字符串里的word替换为python1用字符串本身 ...

  4. python字符串替换的2种方法

    python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'把a字符串里的word替换为python 1.用字符串本 ...

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

    说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...

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

    import re if __name__ == "__main__": url = " \n deded<a href = "">这是第 ...

  7. python字符串截取与替换的例子

    python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...

  8. Python字符串内建处理函数

    #coding=utf-8 __author__ = 'Administrator' # 字符串处理函数 s1 = "study python string function , I lov ...

  9. python 字符串处理

    介绍字符串相关的:比较,截取,替换,长度,连接,反转,编码,格式化,查找,复制,大小写,分割等操作 什么是字符串 字符串 字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s= ...

随机推荐

  1. AliRedis性能

    引言:         如今redis凭借其高性能的优势, 以及丰富的数据结构作为cache已越来越流行, 逐步取代了memcached等cache产品, 在Twitter,新浪微博中广泛使用,阿里巴 ...

  2. 2016 acm香港网络赛 C题. Classrooms(贪心)

    原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and ...

  3. phpstudy配置php7.1.11 + phpstudy nginx伪静态

    切记要把新的php版本配到环境变量,cmd才会生效 php7.1.11下载地址 http://windows.php.net/download/ 下载之后,解压. 重名的为php-7.1.11-nts ...

  4. 如何正确对tomcat host进行配置

    今天在对tomcat的host容器(即虚拟主机的配置)进行配置时,发现即使修改了host name的值(默认为localhost),但是仍无法访问web项目的问题(提示域名解析出错).只能使用默认的值 ...

  5. hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  6. 【BZOJ3112】[Zjoi2013]防守战线 单纯形法

    [BZOJ3112][Zjoi2013]防守战线 题解:依旧是转化成对偶问题,然后敲板子就行了~ 建完表后发现跟志愿者招募的表正好是相反的,感觉很神奇~ #include <cstdio> ...

  7. Zabbix-Agent 客户端安装配置

    1.安装Zabbix官方的yum源 [root@crazy-acong ~]# rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zab ...

  8. Python锁

    # coding:utf-8 import threading import time def test_xc(): f = open("test.txt","a&quo ...

  9. ubuntun下安装Fiddler

    对于分析网页或者写爬虫的时候经常需要用到抓包工具进行网页数据的抓包.在Windows下可以安装Fiddler来抓包.在ubuntun下不能直接安装Fiddler.需要先安装mono 1 首先安装mon ...

  10. 阿里云centos7搭建php+nginx环境

    阿里云Centos搭建lnmp(php7.1+nginx+mysql5.7) https://jingyan.baidu.com/article/215817f7a10bfb1eda14238b.ht ...