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 ...
随机推荐
- Android studio的深坑 导jar包重复的异常处理
导包重复这个问题折磨了整整一天!! 第一个方法在gradle文件下看看有没有重复的包 如果有那么一切都是浮云分分钟搞定 ,最可恶的是在gradle里面没有. 在gradle里面找不到的话就要考虑一下 ...
- 《Java中的自动装箱和拆箱功能.》
//Java中的自动装箱和拆箱功能. class AutoboxingUnboxing { public static void main(String[] args) { //直接把一个基本类型变量 ...
- UIkit框架之UISegmentedControl
1.继承链:UIcontrol:UIview:uiresponder:NSObject 2.初始化 (1)- (instancetype)initWithItems:(NSArray *)items ...
- json_decode时含有中文是解码问题(json_decode返回为null)
function myDecode($str){ $str = substr(str_replace('\"','"',json_encode($str)),1,-1); retu ...
- R正则表达式的问题
今天在处理R的正则表达式的时候发现,R的正则式中的转义字符和linux.python等的还不一样. Linux是使用"\",而R中则使用"[]"! # 我想要将 ...
- 移动互联网实战--资源类APP的数据存储处理和优化
前言: 对于资源类的APP, 其音频/图形占据了APP本身很大的比例. 如何存储和管理这些资源文件, 成了一个颇具挑战性的难点. 移动端的碎片化, 高中低端手机的并存, 需要开发者不光是具备基础的存储 ...
- 【ZJOI2004】嗅探器
练tarjian不错的题,连WA几次后终于会记住tarjian的模板了 原题: 某军搞信息对抗实战演习.红军成功地侵入了蓝军的内部网络.蓝军共有两个信息中心.红军计划在某台中间服务器上安装一个嗅探器, ...
- I/O扩展篇(基于74HC164/74HC165)
在我们的单片机应用系统中,常常会遇到I/O口不够的情况.譬如说接有外部RAM而且要求有16个以上的按键,8位数码管以上的显示.而且还不包括其它的外围器件.这时整个系统的I/O资源就很吃紧了.系统的扩展 ...
- jQuery radio change事件 checkbox选中事件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CentOS 7 安装 Oracle 11g
新到的云主机环境,系统是CentOS 7 x86_64,需要安装上安装Oracle11.2g.摸索很长时间,终于折腾搞定了. 下载 Oracle 下载地址:Oracle 11.2.0.2 (因为不是已 ...