python正则表达式 re (二)sub
2015-08-11 10:20
746 查看
背景:
re.sub是re模块重要的组成部分,并且功能也非常强大,主要功能实现正则的替换。
re.sub定义:
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.
主要的意思为:对字符串string按照正则表达式pattern,将string的匹配项替换成字符串repl。
公式解析:
pattern为表示正则中的模式字符串,
repl为replacement,被替换的内容,repl可以是字符串,也可以是函数。
string为正则表达式匹配的内容。
count:由于正则表达式匹配到的结果是多个,使用count来限定替换的个数(顺序为从左向右),默认值为0,替换所有的匹配到的结果。
flags是匹配模式,可以使用按位或’|’表示同时生效,也可以在正则表达式字符串中指定。(详情见链接)
举例:
详解:首先导入re模块,使用re.sub函数,r’\w+’为正则表达式,匹配英文单词或数字,’10’为被替换的内容,”ji 43 af,geq”为re匹配的字符串内容,count为2 只替换前两个,flags=re.I 忽略大小写。
输出部分自行理解。
re.sub是re模块重要的组成部分,并且功能也非常强大,主要功能实现正则的替换。
re.sub定义:
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.
主要的意思为:对字符串string按照正则表达式pattern,将string的匹配项替换成字符串repl。
公式解析:
pattern为表示正则中的模式字符串,
repl为replacement,被替换的内容,repl可以是字符串,也可以是函数。
string为正则表达式匹配的内容。
count:由于正则表达式匹配到的结果是多个,使用count来限定替换的个数(顺序为从左向右),默认值为0,替换所有的匹配到的结果。
flags是匹配模式,可以使用按位或’|’表示同时生效,也可以在正则表达式字符串中指定。(详情见链接)
举例:
>import re >re.sub(r'\w+','10',"ji 43 af,geq",2,flags=re.I) '10 10 af,geq'
详解:首先导入re模块,使用re.sub函数,r’\w+’为正则表达式,匹配英文单词或数字,’10’为被替换的内容,”ji 43 af,geq”为re匹配的字符串内容,count为2 只替换前两个,flags=re.I 忽略大小写。
输出部分自行理解。
相关文章推荐
- python---socket模块
- python 操作mysql 中文乱码
- Python开发环境配置
- 地牢第一关(Python)
- python垃圾回收
- (!)Python 各种测试框架简介
- 零基础学python-2.4 操作符
- 零基础学python-2.4 操作符
- 简单讲解Python中的闭包
- 零基础学python-2.3 注释
- 零基础学python-2.3 注释
- Python学习笔记1——搭建环境与“Hello World”
- python中字符串内置方法整理
- python list.remove(),del()和filter & lambda
- 简单讲解Python中的闭包
- Python UnitTest 框架执行功能自动化测试
- 零基础学python-2.2 输入 input()
- 零基础学python-2.2 输入 input()
- Python绘制excel表格到邮件正文区并发送脚本实例
- python注释