re模块是python中处理正在表达式的一个模块

正则表达式知识储备:http://www.cnblogs.com/huamingao/p/6031411.html

1. match(pattern, string, flags=0)

从字符串的开头进行匹配, 匹配成功就返回一个匹配对象,匹配失败就返回None

flags的几种值
X 忽略空格和注释

I 忽略大小写的区别   case-insensitive matching

S  . 匹配任意字符,包括新行

def match(pattern, string, flags=0):
"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).match(string)

match(pattern, string, flags=0)

2. search(pattern, string, flags=0)

浏览整个字符串去匹配第一个,未匹配成功返回None

def search(pattern, string, flags=0):
"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).search(string)

search(pattern, string, flags=0)

3. findall(pattern, string, flags=0)

match和search均用于匹配单值,即:只能匹配字符串中的一个,如果想要匹配到字符串中所有符合条件的元素,则需要使用 findall。

findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;空的匹配也会包含在结果中

def findall(pattern, string, flags=0):
"""Return a list of all non-overlapping matches in the string. If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group. Empty matches are included in the result."""
return _compile(pattern, flags).findall(string)

findall(pattern, string, flags=0)

4. sub(pattern,repl,string,count=0,flags=0)

替换匹配成功的指定位置字符串

def 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."""
return _compile(pattern, flags).sub(repl, string, count)

sub(pattern, repl, string, count=0, flags=0)

5. split(pattern,string,maxsplit=0,flags=0)
根据正则匹配分割字符串

def split(pattern, string, maxsplit=0, flags=0):
"""Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings. If
capturing parentheses are used in pattern, then the text of all
groups in the pattern are also returned as part of the resulting
list. If maxsplit is nonzero, at most maxsplit splits occur,
and the remainder of the string is returned as the final element
of the list."""
return _compile(pattern, flags).split(string, maxsplit)

split(pattern, string, maxsplit=0, flags=0)

6.compile()

python代码最终会被编译为字节码,之后才被解释器执行。在模式匹配之前,正在表达式模式必须先被编译成regex对象,预先编译可以提高性能,re.compile()就是用于提供此功能。

7. group()与groups()

匹配对象的两个主要方法:

group()  返回所有匹配对象,或返回某个特定子组,如果没有子组,返回全部匹配对象

groups() 返回一个包含唯一或所有子组的的元组,如果没有子组,返回空元组

    def group(self, *args):
"""Return one or more subgroups of the match. :rtype: T | tuple
"""
pass def groups(self, default=None):
"""Return a tuple containing all the subgroups of the match, from 1 up
to however many groups are in the pattern. :rtype: tuple
"""
pass

Group() and Groups()

python与正则表达式:re模块详解的更多相关文章

  1. python中正则表达式re模块详解

    正则表达式是处理字符串的强大工具,它有自己特定的语法结构,有了它,实现字符串的检索,替换,匹配验证都不在话下. 当然,对于爬虫来说,有了它,从HTML里提取想要的信息就非常方便了. 先看一下常用的匹配 ...

  2. Python 单向队列Queue模块详解

    Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...

  3. (转)python之os,sys模块详解

    python之sys模块详解 原文:http://www.cnblogs.com/cherishry/p/5725184.html sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和 ...

  4. python里面的xlrd模块详解(一)

    那我就一下面积个问题对xlrd模块进行学习一下: 1.什么是xlrd模块? 2.为什么使用xlrd模块? 3.怎样使用xlrd模块? 1.什么是xlrd模块? python操作excel主要用到xlr ...

  5. python里面的xlrd模块详解

    那我就一下面积个问题对xlrd模块进行学习一下: 1.什么是xlrd模块? 2.为什么使用xlrd模块? 3.怎样使用xlrd模块? 1.什么是xlrd模块? ♦python操作excel主要用到xl ...

  6. Python自学笔记-logging模块详解

    简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...

  7. python进阶之time模块详解

    Time模块 Time模块包含的函数 Time模块包含了一下内置的函数,既有时间处理的,也有转换时间格式的: 序号 函数及描述 1 time.altzone 返回格林威治西部的夏令时地区的偏移秒数.如 ...

  8. python--requests模块详解

    GET请求 首先构造一个最简单的get请求,请求的链接为http://httpbin.org/get import requests 2 r = requests.get("http://h ...

  9. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  10. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

随机推荐

  1. android:LayoutInflater

    LayoutInflater:一般用于查找res/layout下的布局文件,findViewById()一般是用于查找布局下的各种控件 一般:我们使用LayoutInflater.from(conte ...

  2. C++ Daily 《1》----关于对象

    1. 问题 请问如下的一个 class 的一个对象占了多少内存? 具体包含哪些东西? non-static 变量? static member 变量? member function?? virtua ...

  3. GCD的其他方法

    1.栅栏函数 作用:控制线程的执行顺序 注:栅栏函数不能使用全局并发队列 -(void)barrier { //1.创建队列(并发队列) dispatch_queue_t queue = dispat ...

  4. C#数据结构

    数据结构是相互之间存在一种或多种特定关系的数据元素的集合.在任何问题中,数据元素之间都不是孤立的,而是存在着一定的关系,这种关系称为结构(Structure).根据数据元素之间关系的不同特性,通常有 ...

  5. 转: SQL Server索引的维护 - 索引碎片、填充因子

    转:http://www.cnblogs.com/kissdodog/archive/2013/06/14/3135412.html 实际上,索引的维护主要包括以下两个方面: 页拆分 碎片 这两个问题 ...

  6. 防止特殊html字符的问题(xxs攻击)方法

    快速对字符转义,避免跨站攻击XSS   XSS已经成为非常流行的网站攻击方式,为了安全起见,尽量避免用户的输入.可是有些情况下不仅不避免,反而要求鼓励输入,比如写博客.博客园开放性很高,可以运行手写的 ...

  7. Directory的GetFiles方法

    想实现一个功能 :比如多个业务审批流程公用一个审批表的时候,有一个提示审批信息的页面 ,点击该页面不同的业务审批流程记录的时候,跳转到不同业务流程的详细显示界面 额 这样说 貌似以后我自己也看不明白 ...

  8. RC上电复位时间计算

    高电平复位电路图 V0 为电容上的初始电压值:V1 为电容最终可充到或放到的电压值:Vt 为t时刻电容上的电压值.则,    Vt="V0"+(V1-V0)* [1-exp(-t/ ...

  9. mysql登陆出现unknown database错误可能原因

    输入了错误命令如 # mysql -u root -p test 然后客户端会出现需要输入命令的提示,即使输入正确出现错误提示 正确命令是 # mysql -u root -p

  10. IO-同步,异步,阻塞,非阻塞

    IO-同步,异步,阻塞,非阻塞1.什么是IO数据在系统内核(kernel)和用户进程之间的传递,称为IO. 2.IO操作步骤以read为例,涉及两个系统对象,调用IO的process(or threa ...