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)

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)

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)

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)

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)

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

原文地址:http://www.cnblogs.com/huamingao/p/6062367.html

python re模块详解_python与正则表达式:re模块详解相关推荐

  1. python中文字符串多余空格_python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解...

    python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解 在pdf转为文本的时候,经常会多出空格,影响数据观感,因此需要去掉文本中多余的空格,而文本中的英文之间的正常空格需要保留,输入 ...

  2. python中模块和函数_Python中函数和模块的体验与使用

    函数基础 目标 函数的快速体验 函数的基本使用 函数的参数 函数的返回值 函数的嵌套调用 在模块中定义函数 01. 函数的快速体验 1.1 快速体验 所谓函数,就是把 具有独立功能的代码块 组织为一个 ...

  3. python模块编程教程_python进阶教程之模块(module)介绍

    我们之前看到了函数和对象.从本质上来说,它们都是为了更好的组织已经有的程序,以方便重复利用. 模块(module)也是为了同样的目的.在Python中,一个.py文件就构成一个模块.通过模块,你可以调 ...

  4. python查询模块所有类_python 小技巧(import模块、查询类继承关系、安装包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在这里列举一些我使用Python时积累的小技巧.这些技巧是我在使用Python过程 ...

  5. python导入模块报错_Python 导入上层目录模块报错

    背景: 当前demo.py 文件,所处目录 D:\py\test\TestCase,需要调用test 目录下的模块,尝试了 新建__init__.py 文件+ import test.模块名的方法,无 ...

  6. python中configparser详解_Python中的ConfigParser模块使用详解

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  7. python正则去掉重复单词_python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解 | 学步园...

    在pdf转为文本的时候,经常会多出空格,影响数据观感,因此需要去掉文本中多余的空格,而文本中的英文之间的正常空格需要保留,输入输出如下: input:我今天 赚了 10 个亿,老百姓very happ ...

  8. python argparse模块详解_python学习之argparse模块

    一.介绍 argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行参数. 我们很多时候,需要用到解析命令行参 ...

  9. python nmap模块详解_python中的Nmap模块问题

    我已经安装了nmap.exe文件以及nmap模块.但我不确定如何配置nmap路径. 输入nmap路径的代码块如下所示class PortScanner(object): ""&qu ...

最新文章

  1. 海蜘蛛如何手工升级到最新版
  2. rails笔记 cache系统
  3. c语言课程设计怎么做,C语言课程设计————写下流程图! 谢谢
  4. fasttext初步使用
  5. Docker中搭建FastDFS文件系统(多图)
  6. Linux学习笔记-Makefile的基本使用
  7. 计算机无法从硬盘启动怎么办,电脑不能从硬盘启动应该怎么解决
  8. 手机登入注册为什么出现这个错误?
  9. 解决0RA-04031故障
  10. 如何使用火狐下的两款接口测试工具RESTClient和HttpRequester发送post请求
  11. Win10数字签名错误/winload.exe 错误解决办法
  12. 蚁群算法讲解python
  13. edp和edt哪个好_解密香水瓶上edt和edp分别代表什么,以及常见香水的分类!
  14. 学算法怎么样?算法工程师薪资前景好吗?
  15. MIT-BIH介绍(二)什么是MIT-BIH?
  16. 掌握电商后台设计,这一篇足矣
  17. 使用bootstrap来模拟构建Cropper的官方网站
  18. 大悲寺——依教奉行溯正源,良苦用心谁人知?纵然世间一比丘,不退初心证菩提。[转]...
  19. 批量将swa文件转成MP3文件
  20. 台式计算机摄像头怎么打开,台式机摄像头的打开方法

热门文章

  1. 机器学习真的可以起作用吗?(1)
  2. [笔记]kubernetes 无法启动问题
  3. “模板”学习笔记(7)-----数组模板+对象数组举例
  4. asp.net试题(三)
  5. 10-11-根据文章标题搜索文章
  6. android studio 多个项目管理,Android Studio之同一应用创建多个Activity(一)
  7. java aop xml配置_spring AOP使用 xml配置
  8. java nio 连接数_Java NIO 基础一 NIO概念
  9. 测速源码_解密,相亲交友直播系统源码,高并发如何做到不卡顿
  10. linux编译minix,MINIX对Linux