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. UIRefreshControl自动刷新

    不知道UIRefreshController是什么的朋友可以参考iOS6新特征:UIRefreshControl[下拉刷新]使用示例 一文了解这是什么,这里只提怎么使用代码的方式触发UIRefresh ...

  2. REDIS key notification

    Commands Clients Documentation Community Download Support License Join us in London October 19th for ...

  3. ecshop安装常见问题及解决办法

    一,Ecshop首页出现报错:Only variables should be passed by referen 最近想安装一个ECSHOP商城上去,老是报错,出现下面这就话: Strict Sta ...

  4. Azure媒体服务的Apple FairPlay流功能正式上线

    在此我们高兴地宣布,Azure FairPlay Streaming服务已正式商用. FairPlay允许用户轻松构建解决方案,并可扩展到最新版本的Apple TV.Azure媒体服务可以结合现有的P ...

  5. 【转载】张逸--ThoughtWorks(中国)程序员读书雷达

    原文地址:ThoughtWorks(中国)程序员读书雷达 软件业的特点是变化.若要提高软件开发的技能,就必须跟上技术发展的步伐.埋首醉心于项目开发与实战,固然能够锤炼自己的开发技巧,却难免受限于经验与 ...

  6. UITableView 接口的调用顺序

    ios7启用estimatedHeightForRowAtIndexPath之后的api调用顺序called -[XHYTableViewController tableView:heightForR ...

  7. Flash Builder如何自定义工作目录

    熟悉了myeclipse可以自定义目录的设置,今天在使用flash builder 时,当导入一个工程到现有项目空间 选择根目录,点击浏览的时候出现的目录是C:\Users\Administrator ...

  8. (转) 一张图解AlphaGo原理及弱点

    一张图解AlphaGo原理及弱点 2016-03-23 郑宇,张钧波 CKDD 作者简介: 郑宇,博士, Editor-in-Chief of ACM Transactions on Intellig ...

  9. F1 分数

    F1 分数会同时考虑精确率和召回率,以便计算新的分数. 可将 F1 分数理解为精确率和召回率的加权平均值,其中 F1 分数的最佳值为 1.最差值为 0: F1 = 2 * (精确率 * 召回率) / ...

  10. UISwitch控件的使用

    UISwitch控件的作用是提供一个开关给用户,用户可以选择打开或者关闭. UISwitch的基本属性包括: 1.onTintColor:打开状态下的背景颜色 2.thumbTintColor:滑块的 ...