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 ...
随机推荐
- bootstrap 响应式布局 居中问题
大家能够通过table来设置居中: display: table; width: auto; margin-left: auto; margin-right: auto;
- Drupal 7 建站学习手记(四):怎样改动Nivo Slider模块的宽高
背景 Nivo Slider模块默认大小是用的height: 100%, width 100%, 但IE7及下面的浏览器是不支持百分比宽高的, 而我的项目目标用户基本都是使用XP系统,项目需求是必须兼 ...
- TASKKILL命令使用方法
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM ima ...
- 简单的Session登录
Login前台页面 <form id="form1" action ="" method="post"> <input t ...
- log4j 详解
转载自:http://www.blogjava.net/hwpok/archive/2008/08/23/223891.html >>>>1. 概述<<< ...
- “Zhuang.Data”轻型数据库访问框架(一)开篇介绍
目录: “Zhuang.Data”轻型数据库访问框架(一)开篇介绍 “Zhuang.Data”轻型数据库访问框架(二)框架的入口DbAccessor对象 框架介绍 该框架主要用于数据库访问,封装了包括 ...
- JSP标签库
step1: 定义一个标签处理类:改类必须实现SimpleTag接口,在实际中是拓展它的子类SimpleTagSupport.复写doTag方法 public class MyTag extends ...
- BZOJ 1146: [CTSC2008]网络管理Network( 树链剖分 + 树状数组套主席树 )
树链剖分完就成了一道主席树裸题了, 每次树链剖分找出相应区间然后用BIT+(可持久化)权值线段树就可以完成计数. 但是空间问题很严重....在修改时不必要的就不要新建, 直接修改原来的..详见代码. ...
- Linux命令之切换用户
一.从 user 用户切换到 root 用户 不管是用图形模式登录 Ubuntu,还是命令行模式登录,我们会发现缺省的用户是 user,但是当我们需要执行一些具有 root 权限的操作(如修还系统文件 ...
- jQuery入门第二
element选择器 在文具盒中,有铅笔.钢笔和水彩笔,类似于页面中的<div>.<span>各个元素,虽然同属于一个容器,但有各自的功能,jQuery中可以根据元素名查找元 ...