使用 Python 模块 re 实现解析小工具
2017-12-21 11:34
756 查看
Python re 的主要功能
Python 的 re 正则表达式模块定义了一系列函数,常量以及异常;同时,正则表达式被编译成‘ RegexObject ’实例,本身可以为不同的操作提供方法。接下来简要介绍一下这些函数的功能和用法。compile
re.compile(pattern[, flags])
把正则表达式的模式和标识转化成正则表达式对象,供 match() 和 search() 这两个函数使用。
re 所定义的 flag 包括:
re.I 忽略大小写
re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境
re.M 多行模式
re.S 即为’ . ’并且包括换行符在内的任意字符(’ . ’不包括换行符)
re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库
re.X 为了增加可读性,忽略空格和’ # ’后面的注释
例:以下两种用法结果相同:
A)
re.search(pattern, string[, flags])
在字符串中查找匹配正则表达式模式的位置,返回 MatchObject 的实例,如果没有找到匹配的位置,则返回 None。
对于已编译的正则表达式对象来说(re.RegexObject),有以下 search 的方法:
search (string[, pos[, endpos]])
若 regex 是已编译好的正则表达式对象,regex.search(string, 0, 50) 等同于 regex.search(string[:50], 0)。
具体示例如下。
>>> pattern = re.compile("a") >>> pattern.search("abcde") # Match at index 0 >>> pattern.search("abcde", 1) # No match; |
re.match(pattern, string[, flags])
判断 pattern 是否在字符串开头位置匹配。对于 RegexObject,有:
match(string[, pos[, endpos]])
match() 函数只在字符串的开始位置尝试匹配正则表达式,也就是只报告从位置 0 开始的匹配情况,而 search() 函数是扫描整个字符串来查找匹配。如果想要搜索整个字符串来寻找匹配,应当用 search()。
split
re.split(pattern, string[, maxsplit=0, flags=0])
此功能很常用,可以将将字符串匹配正则表达式的部分割开并返回一个列表。对 RegexObject,有函数:
split(string[, maxsplit=0])
例如,利用上面章节中介绍的语法:
re.findall(pattern, string[, flags])
在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回。同样 RegexObject 有:
findall(string[, pos[, endpos]])
示例如下:
re.finditer(pattern, string[, flags])
和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并组成一个迭代器返回。同样 RegexObject 有:
finditer(string[, pos[, endpos]])
sub
re.sub(pattern, repl, string[, count, flags])
在字符串 string 中找到匹配正则表达式 pattern 的所有子串,用另一个字符串 repl 进行替换。如果没有找到匹配 pattern 的串,则返回未被修改的 string。Repl 既可以是字符串也可以是一个函数。对于 RegexObject 有:
sub(repl, string[, count=0])
此语法的示例有:
>>> p.sub( 'num', ' one word two words three words', count=1)
' num word two words three words'
subn
re.subn(pattern, repl, string[, count, flags])
该函数的功能和 sub() 相同,但它还返回新的字符串以及替换的次数。同样 RegexObject 有:
subn(repl, string[, count=0])
参考 Python 使用文档中 RE 相关章节 The Python
Standard Library - Regular expression operations,查看 RE 语法使用细节。
参看文章 Regular Expressions Primer,了解更多 Python RE 的使用实例。
参看 developerWorks 中的文章 可爱的 Python:Python 中的文本处理,了解使用 Python RE 进行文本解析的实例。
相关文章推荐
- 使用 Python 模块 re 实现解析小工具
- 使用 Python 模块 re 实现解析小工具
- 使用 Python 模块 re 实现解析小工具
- 建议13:使用Python模块re实现解析小工具
- 使用 Python 模块 re 实现解析小工具
- Python使用re模块实现信息筛选的方法
- Python 3.x--使用re模块,实现计算器运算实例
- python re模块实现正则表达式
- Python:使用threading模块实现多线程编程
- 使用VC内嵌Python实现的一个代码检测工具
- Python:使用threading模块实现多线程编程三[threading.Thread类的重要函数]
- Python:使用threading模块实现多线程编程六[可重入锁RLock]
- Python:使用threading模块实现多线程编程八[使用Event实现线程间通信]
- 使用python实现一个通用协议测试工具
- Python:使用threading模块实现多线程编程七[使用Condition实现复杂同步]
- Python:使用threading模块实现多线程编程四[使用Lock互斥锁]
- Python:使用threading模块实现多线程编程四[使用Lock互斥锁]
- Python:使用threading模块实现多线程编程四[使用Lock互斥锁]
- Python:使用threading模块实现多线程编程六[可重入锁RLock]
- Python:使用threading模块实现多线程编程一[综述]