最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python实现查找匹配项作处理后再替换回去的方法
时间:2017-06-14 编辑:简简单单 来源:一聚教程网
本文实例讲述了Python实现查找匹配项作处理后再替换回去的方法。分享给大家供大家参考,具体如下:
这里实现Python在对找到的匹配项进行适当处理后,再替换掉原来那个匹配的项。
代码如下 | 复制代码 |
#!/usr/bin/python # coding=GBK importre # 对m作适当处理后返回结果 deffun(m): print("in: %s"%m.group(0)) ret=m.group(0).upper()[::-1] returnret src="what [can] I do for can you[can] come on" pat="(?<= )(can)(?= )" #print(re.search(pat, src).group(1)) #result = re.sub(pat,lambda m:m.group(1).upper()[::-1], src) # 使用lambda result1=re.sub(pat,lambdam:m.group(0).upper()[::-1], src) print("result1: %s\n"%result1) # 在re.sub中使用函数 result2=re.sub(pat, fun, src) print("result2: %s"%result2) |
运行输出:
代码如下 | 复制代码 |
[zcm@python#112]$./del.py result1: what [NAC] I doforcan you[NAC] come on in: can in: can result2: what [NAC] I doforcan you[NAC] come on [zcm@python#113]$ |
-
上一个: scrapy的基本命令介绍
-
下一个: Python中扩展包如何安装
相关文章
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31
- php双向队列解读 10-31
- Laravel异常上下文解决教程 10-24
- php数组查询元素位置方法介绍 10-24