关于python 的re.sub用法
来源:互联网 发布:移动数据开着不能上网 编辑:程序博客网 时间:2024/05/29 10:12
>>> import re>>> text = "JGood is a handsome boy, he is cool, clever, and so on...">>> print(re.sub(r'\s+', '-', text))JGood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on...>>> print(re.sub(r'is\s+', '-', text))JGood -a handsome boy, he -cool, clever, and so on...>>> print(re.sub(r'\s+\.', '.', text))JGood is a handsome boy, he is cool, clever, and so on...>>> text = "JGood is a handsome boy , he is cool , clever , and so on...">>> print(re.sub(r'\s+,\s+', ',',text))JGood is a handsome boy,he is cool,clever,and so on...>>>
许多资料的介绍如下:
re.sub
re.sub用于替换字符串中的匹配项。下面一个例子将字符串中的空格 ’ ’ 替换成 ‘-’ :
- import re
- text = ”JGood is a handsome boy, he is cool, clever, and so on…”
- print re.sub(r‘\s+’, ‘-‘, text)
re.sub的函数原型为:re.sub(pattern, repl, string, count)
其中第二个函数是替换后的字符串;本例中为’-‘
第四个参数指替换个数。默认为0,表示每个匹配项都替换。
re.sub还允许使用函数对匹配项的替换进行复杂的处理。如:re.sub(r’\s’, lambda m: ‘[’ + m.group(0) + ‘]’, text, 0);将字符串中的空格’ ‘替换为’[ ]’。
自己实验了一下,结果的确把句子中的“ ”替换为“-”
>>> text = “JGood is a handsome boy, he is cool, clever, and so on…”
JGood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on…
好奇之下,把“ r’\s+’ ” 替换为“ r’is\s+’” 结果是把原句中的is改为了-
>>> text = “JGood is a handsome boy, he is cool, clever, and so on…”
>>> print re.sub(r’is\s+’, ‘-‘, text)JGood -a handsome boy, he -cool, clever, and so on…
自己的开源项目中用到了re.sub(r’\s+,\s+’, ‘, ‘, text),难道是把”,”改为逗号,这没有什么用处啊,很好奇,继续实验,结果如下
>>> print re.sub(r’\s+\.’, ‘.’, text)
JGood is a handsome boy, he is cool, clever, and so on…
确实,如果用这个例句,没有任何更改。
不死心,就把例句做了一些更改,多家几个逗号试试。
>>> text=”JGood is a handsome boy, , , he is cool, clever, and so on…”
>>> print re.sub(r’\s+,\s+’, ‘, ‘, text)
JGood is a handsome boy,, , he is cool, clever, and so on…
发现,三个逗号没有少,是空格发生了变化。
于是继续探索,在原句每个空格之前加了空格,继续实验
>>> text = “JGood is a handsome boy , he is cool , clever , and so on…”
>>> print re.sub(r’\s+,\s+’, ‘,’, text)
JGood is a handsome boy,he is cool,clever,and so on…
哈哈,原来是把“,”前后的空格给删除了。 顿时领悟了re.sub(pattern, repl, string, count)中PATTERN的作用,找到text中与patern所匹配的形式,把text中与patern所匹配的形式以外的用repl代替。
>>> text = “JGood is a handsome boy , he is cool , clever . and so on…”
>>> print re.sub(r’\s+\.’, ‘.’, text)
JGood is a handsome boy , he is cool , clever. and so on…
很明显“clever ”后句号前空格被去除。
- 关于python 的re.sub用法
- 关于python 的re.sub用法
- re.sub的用法
- python的re模块的sub方法
- python re sub
- python re.sub
- Python re.sub
- python re.sub 正则匹配
- 详解Python中re.sub
- python:re.sub简要分析
- 详解Python中re.sub
- 详解Python中re.sub
- python正则表达式模块,re.sub的函数使用
- python中re.sub的使用 正则表达式
- 关于Python中,re.sub(pattern, repl, string, count=0, flags=0)方法的个人理解
- Python--详解Python中re.sub
- python re模块 sub方法介绍
- Python开发——re.sub
- 数据库中的sql优化
- 团队背包
- NPAPI学习记录
- POJ 3468(线段树模板 Lazy)
- Linux添加头文件路径
- 关于python 的re.sub用法
- mobilenet压缩
- Spark UI无法查看到slave节点
- 小程序的一些问题
- Linux系统(centOS)修改时区
- 第一个js +html程序
- php ajax
- 第六场选拔赛 Greatest Naruto Army DP+递归
- 1.11Android 学习+进度之十一-多人聊天缓存