参考文档: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(正则模块)的更多相关文章

  1. Python中正则模块re.compile、re.match及re.search函数用法

    import rehelp(re.compile)'''输出结果为:Help on function compile in module re: compile(pattern, flags=0) C ...

  2. python re正则模块

    re 正则表达式操作  本模块提供了类似于Perl的正则表达式匹配操作.要匹配的模式和字符串可以是Unicode字符串以及8位字符串. 正则表达式使用反斜杠字符('\')来表示特殊的形式或者来允许使用 ...

  3. python的正则re模块

    一. python的正则 python的正则模块re,是其内置模块,可以直接导入,即import re.python的正则和其他应用的正则及其相似,有其他基础的话,学起来还是比较简单的. 二. 正则前 ...

  4. python的re模块详解

    一.正则表达式的特殊字符介绍 正则表达式 ^ 匹配行首 $ 匹配行尾 . 任意单个字符 [] 匹配包含在中括号中的任意字符 [^] 匹配包含在中括号中的字符之外的字符 [-] 匹配指定范围的任意单个字 ...

  5. Python全栈开发【re正则模块】

    re正则模块 本节内容: 正则介绍 元字符及元字符集 元字符转义符 re模块下的常用方法 正则介绍(re) 正则表达式(或 RE)是一种小型的.高度专业化的编程语言. 在Python中,它内嵌在Pyt ...

  6. 小白的Python之路 day5 re正则模块

    re正则模块 一.概述 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,要讲他的具体用法要讲一本书!它内嵌在Python中,并通过 re 模块实现.你可以为想要匹配的相应字符串 ...

  7. Python开发【模块】:re正则

    re模块 序言: re模块用于对python的正则表达式的操作 '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags ...

  8. 认识python正则模块re

    python正则模块re python中re中内置匹配.搜索.替换方法见博客---python附录-re.py模块源码(含re官方文档链接) 正则的应用是处理一些字符串,phthon的博文python ...

  9. python - re正则匹配模块

    re模块 re 模块使 Python 语言拥有全部的正则表达式功能. compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象.该对象拥有一系列方法用于正则表达式匹配和替换. re ...

随机推荐

  1. 魔兽世界---屠夫(Just a Hook)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. C++四种强制类型转换详解

    什么是类型转换? 类型转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式.为了类型转换一个简单对象为另一个对象你会使用传统的类型转换操作符. C与C++的类型转换 //C中: //复 ...

  3. select实现选中跳转

    select选择后直接跳转到其他网站的三种方式     第一种: ************************** <html> <head> <meta http- ...

  4. 【转】SQL Server 2008 新类型介绍之Date和Time

     转自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/13/4344429.aspx   SQL Server 2008除了 ...

  5. localhost简介、localhost与 127.0.0.1 及 本机IP 的区别

    localhost是什么意思? 相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析. 看来这个入门问题还有人不清楚,其实这两者是有区别的. localhos ...

  6. SQLserver数据库操作帮助类SqlHelper

    1 SqlHelper源码 using System; using System.Data; using System.Xml; using System.Data.SqlClient; using ...

  7. Unity5UGUI 官方教程学习笔记(四)UI Image

    Image Source image:源图片  需要显示的图片 Color:颜色  会与图片进行颜色的混合 Material:材质 Image Type:  Simple   精灵只会延伸到适合Rec ...

  8. Ubuntu的快捷键

    正如大家都知道的那样,Ubuntu的终端的Terminal能快捷的操作该linux系统,减少鼠标的使用.(vim党,尽量避免使用鼠标) 在Ubuntu中,终端的快捷键为(大小写无关的): Ctrl + ...

  9. SQL Server 中大小写区分的处理

    SQL Server 中大小写区分的处理. 默认情况下,SQL Server 里面是不区分大小写的: E:\>sqlcmd -S "localhost\SQLEXPRESS" ...

  10. 阿里云ECS每天一件事D7:安装tomcat8.0

    这一D,跨越了几个月啊,人是越来越懒,集中写一些,就懒得再记录了.也是因为测试需要,搭建个jsp的服务环境,只是测试,考虑用tomcat就够了. 在Tomcat官网下载最新Core版本,下载之后,将文 ...