python与正则表达式:re模块详解
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)
根据正则匹配分割字符串
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模块详解的更多相关文章
- python中正则表达式re模块详解
正则表达式是处理字符串的强大工具,它有自己特定的语法结构,有了它,实现字符串的检索,替换,匹配验证都不在话下. 当然,对于爬虫来说,有了它,从HTML里提取想要的信息就非常方便了. 先看一下常用的匹配 ...
- Python 单向队列Queue模块详解
Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...
- (转)python之os,sys模块详解
python之sys模块详解 原文:http://www.cnblogs.com/cherishry/p/5725184.html sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和 ...
- python里面的xlrd模块详解(一)
那我就一下面积个问题对xlrd模块进行学习一下: 1.什么是xlrd模块? 2.为什么使用xlrd模块? 3.怎样使用xlrd模块? 1.什么是xlrd模块? python操作excel主要用到xlr ...
- python里面的xlrd模块详解
那我就一下面积个问题对xlrd模块进行学习一下: 1.什么是xlrd模块? 2.为什么使用xlrd模块? 3.怎样使用xlrd模块? 1.什么是xlrd模块? ♦python操作excel主要用到xl ...
- Python自学笔记-logging模块详解
简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...
- python进阶之time模块详解
Time模块 Time模块包含的函数 Time模块包含了一下内置的函数,既有时间处理的,也有转换时间格式的: 序号 函数及描述 1 time.altzone 返回格林威治西部的夏令时地区的偏移秒数.如 ...
- python--requests模块详解
GET请求 首先构造一个最简单的get请求,请求的链接为http://httpbin.org/get import requests 2 r = requests.get("http://h ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
随机推荐
- 【转】使用Sublime + PlantUML高效地画图
project: blog status: publish target: how-to-use-sublime-and-plant-uml-draw-diagram.md date: 2015-12 ...
- js传url中文参数乱码问题
$("#btnKeyWord").click(function () { window.open("/Atraction/Atraction.aspx?keyword=& ...
- c# List 按类的指定字段排序
List<Report> list=Report.FindAllbyClassID(id); ) { list.Sort(delegate(Report x,Report y){ret ...
- .使用 HTML+CSS 实现如图布局,border-widht 5px,一个格子大小是 50*50,hover时候边框变为红色(兼容IE6+,考虑语义化的结构)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 一些sql语句的常用总结(重要)
select primary_flag from tc_contact where primary_flag !=0 select dept_id,dept_name,tree_level,tree_ ...
- Python 决策树的构造
上一节我们学习knn,kNN的最大缺点就是无法给出数据的内在含义,而使用决策树处理分类问题,优势就在于数据形式非常容易理解. 决策树的算法有很多,有CART.ID3和C4.5等,其中ID3和C4.5都 ...
- LintCode Search a 2D Matrix II
排好序的二维数组, 从上到下从左到右增大, 给出一个数找出此数组里有多少个这个数. 不用两个循环做, 着手于条件(从左下角开始,若相等往右上跳一个,若小于target往右边跳一个,若大于target往 ...
- mm/makefile
## Makefile for the linux memory manager.## Note! Dependencies are done automagically by 'make dep', ...
- 我用工具怎么连接不上mysql数据库的? MySql access denied for user错误
MySql access denied for user错误 方法/步骤 MySql远程连接时的"access denied for user **@**"错误,搞的我很头大, ...
- 一加3,CM13蓝牙共享互联网 无效。
一加3准备把4G网络共享给魅族PRO5. 但在一加3的蓝牙设置里怎么勾选都无用. 最后发现在要在PRO5上设置才行. 1.在蓝牙列表中,点击带圈的感叹号. 2.选择“互联网访问”. - --