python re(正则模块)
参考文档:http://blog.csdn.net/wusuopubupt/article/details/29379367
ipython环境中,输入"?re",官方解释如下:
This module exports the following functions:
match Match a regular expression pattern to the beginning of a string.
search Search a string for the presence of a pattern.
sub Substitute occurrences of a pattern found in a string.
subn Same as sub, but also return the number of substitutions made.
split Split a string by the occurrences of a pattern.
findall Find all occurrences of a pattern in a string.
finditer Return an iterator yielding a match object for each match.
compile Compile a pattern into a RegexObject.
purge Clear the regular expression cache.
escape Backslash all non-alphanumerics in a string. Some of the functions in this module takes flags as optional parameters:
I IGNORECASE Perform case-insensitive matching.
L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
M MULTILINE "^" matches the beginning of lines (after a newline)
as well as the string.
"$" matches the end of lines (before a newline) as well
as the end of the string.
S DOTALL "." matches any character at all, including the newline.
X VERBOSE Ignore whitespace and comments for nicer looking RE's.
U UNICODE Make \w, \W, \b, \B, dependent on the Unicode locale.
1. re.compile
compile函数会将一个字符串对象转换为RegexObject,官方示例如下:
import re # Precompile the patterns
regexes = [re.compile(p)
for p in ['this', 'that']
]
text = "Does this text match the pattern?"
print 'Text: %r\n' % repr(text) for regex in regexes:
print 'Seeking "%s"->' % regex.pattern,
if regex.search(text):
print 'match!'
else:
print 'no match'
模块级别函数会维护一个已编译表达式缓存,直接使用已经编译表达式可以减少缓存查找开销,同时在开始时预编译表达式,可以避免运行时在进行编译。节省世间
python re(正则模块)的更多相关文章
- Python中正则模块re.compile、re.match及re.search函数用法
import rehelp(re.compile)'''输出结果为:Help on function compile in module re: compile(pattern, flags=0) C ...
- python re正则模块
re 正则表达式操作 本模块提供了类似于Perl的正则表达式匹配操作.要匹配的模式和字符串可以是Unicode字符串以及8位字符串. 正则表达式使用反斜杠字符('\')来表示特殊的形式或者来允许使用 ...
- python的正则re模块
一. python的正则 python的正则模块re,是其内置模块,可以直接导入,即import re.python的正则和其他应用的正则及其相似,有其他基础的话,学起来还是比较简单的. 二. 正则前 ...
- python的re模块详解
一.正则表达式的特殊字符介绍 正则表达式 ^ 匹配行首 $ 匹配行尾 . 任意单个字符 [] 匹配包含在中括号中的任意字符 [^] 匹配包含在中括号中的字符之外的字符 [-] 匹配指定范围的任意单个字 ...
- Python全栈开发【re正则模块】
re正则模块 本节内容: 正则介绍 元字符及元字符集 元字符转义符 re模块下的常用方法 正则介绍(re) 正则表达式(或 RE)是一种小型的.高度专业化的编程语言. 在Python中,它内嵌在Pyt ...
- 小白的Python之路 day5 re正则模块
re正则模块 一.概述 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,要讲他的具体用法要讲一本书!它内嵌在Python中,并通过 re 模块实现.你可以为想要匹配的相应字符串 ...
- Python开发【模块】:re正则
re模块 序言: re模块用于对python的正则表达式的操作 '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags ...
- 认识python正则模块re
python正则模块re python中re中内置匹配.搜索.替换方法见博客---python附录-re.py模块源码(含re官方文档链接) 正则的应用是处理一些字符串,phthon的博文python ...
- python - re正则匹配模块
re模块 re 模块使 Python 语言拥有全部的正则表达式功能. compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象.该对象拥有一系列方法用于正则表达式匹配和替换. re ...
随机推荐
- Review1-11
泛型 泛型类 Public 类名 class<T>{} 泛型方法 Public static <T> 返回类型 方法名(<T> t){} 泛型类型必须使用引用类型 ...
- The Euler function(欧拉函数)
The Euler function Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- Holding Bin-Laden Captive!(母函数)
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- Android版xx助手之天天酷跑外挂具体分析
Android版xx助手之天天酷跑外挂具体分析 图/文 莫灰灰 背景 近些年来,移动互联网的大肆崛起,潜移默化中影响着人们的生活和工作习惯.当腾讯的微信平台接入手机游戏之后,移动端的游戏也開 ...
- hdu - 1757 - A Simple Math Problem
题意:当x < 10时, f(x) = x: 当x >= 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + + a2 * f(x-3) + …… + a9 ...
- javaScript 工作必知(七) 对象继承
对象继承inherit var o = { r: 1 }; var c = function f() { }; c.prototype = o; c.r = 3; alert(o.r);//被继承的属 ...
- css重置
清除标签的默认样式 body{margin:0;}ul,ol{margin:0 auto;padding:0;}li{ list-style:none;}dl{margin:0;}dd{margin: ...
- java 设计模式初探之适配器模式
1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...
- Nanjing GDG Meetup 10月19日线下活动
Nanjing GDG 10 月份的线下活动将在本周六 (10/19) 举办,这次会请到对所有开发者都有重要帮助的神器 GoAgent 的作者 Phus Lu 来给我们做一场分享,热烈欢迎大家报名参 ...
- 深刻理解一句js
最近有点小懒,不想写东西,也不想看东西,看东西还老跑神,这是肿么了呢? 盯着显示器某一刻能把它看透,就是不想挪下眼睛 无意中看到了这行代码 [].forEach.call($$("*&quo ...