python正则表达式3-模式匹配
re.S,使 '.' 匹配换行在内的所有字符
>>> pattern=r'ghostwu.com'
>>> import re
>>> re.findall( pattern, 'ghostwuacom' )
['ghostwuacom']
>>> re.findall( pattern, 'ghostwubcom' )
['ghostwubcom']
>>> re.findall( pattern, 'ghostwu.com' )
['ghostwu.com']
>>> re.findall( pattern, 'ghostwu\ncom' )
[]
>>> re.findall( pattern, 'ghostwu\ncom', re.S )
['ghostwu\ncom']
>>>
re.M,多行匹配,主要影响( ^和$ )
>>> str="""
... hi,ghostwu,how are you
... ghostwu: my name is ghostwu,how are you
... ghostwu: nice to meet you
... hello ghostwu
... """
>>> pattern = r"^ghostwu"
>>> re.findall( pattern, str )
[]
>>> re.findall( pattern, str, re.M )
['ghostwu', 'ghostwu']
>>>
当正则有多行的时候,可以开启verbose模式re.X
>>> pattern=r"""
... \d{3,4}
... -?
... \d{8}
... """
>>> str="020-88888888"
>>> re.findall( pattern, str )
[]
>>> re.findall( pattern, str, re.X )
['020-88888888']
>>>
():分组与| 的使用, 假如我们要匹配一个.com,.cn,.net结尾的email
>>> pattern=r"\w+@\w+(.com|.cn|.net)"
>>> email="abc@qq.com">>> re.match( pattern, email )
<_sre.SRE_Match object at 0x7f2b74481828>
>>> re.match( pattern, 'abc@qq.cn' )
<_sre.SRE_Match object at 0x7f2b744818a0>
>>> re.match( pattern, 'abc@qq.net' )
<_sre.SRE_Match object at 0x7f2b74481828>
>>> re.match( pattern, 'abc@qq.io' )
>>>
匹配超链接
>>> html="""
... <a href="http://www.baidu.com">百度</a>
... <a href="index.html">首页</a>
... <p>这是一段说明</p>
... <a href="http://www.taobao.com">淘宝</a>
... """
>>> re=r"href=\"(.+?)\""
>>> pattern=r"href=\"(.+?)\""
>>> re
'href=\\"(.+?)\\"'
>>> import re
>>> re.findall( pattern, html )
['http://www.baidu.com', 'index.html', 'http://www.taobao.com']
>>>
python正则表达式3-模式匹配的更多相关文章
- python中正则表达式与模式匹配
一.前言 在之前找工作过程中,面试时经常被问到会不会python,懂不懂正则表达式.心里想:软件的东西和芯片设计有什么关系?咱也不知道因为啥用这个,咱也不敢问啊!在网上搜索到了一篇关于脚本在ASIC领 ...
- Python正则表达式详解
我用双手成就你的梦想 python正则表达式 ^ 匹配开始 $ 匹配行尾 . 匹配出换行符以外的任何单个字符,使用-m选项允许其匹配换行符也是如此 [...] 匹配括号内任何当个字符(也有或的意思) ...
- 比较详细Python正则表达式操作指南(re使用)
比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...
- Python天天美味(15) - Python正则表达式操作指南(re使用)(转)
http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...
- python 正则表达式 学习笔记(不断补充ing)
本文参考了以下博客,感谢众位大神的分享! http://www.oschina.net/question/12_9507 和 http://www.crifan.com/python_re_sub_d ...
- Python 正则表达式(字符)详解
Python正则表达式 - 简介 其实正则表达式这种技术,源于一个很简单的问题: 如何通过变成使得计算机具有在文本中检索某种模式的能力? 而正则表达式为通过编程实现高级的文本模 ...
- python 正则表达式Re
Python正则表达式指南这篇文章很好,推荐阅读. 本文则是简单记录下我自己学习Re的笔记, 环境是python3.5. 1.简单的Re语法 ^ 匹配字符串开始位置. $ 匹配字符串结束位置. \b ...
- python大法好——Python 正则表达式
Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. r ...
- python正则表达式模块re:正则表达式常用字符、常用可选标志位、group与groups、match、search、sub、split,findall、compile、特殊字符转义
本文内容: 正则表达式常用字符. 常用可选标志位. group与groups. match. search. sub. split findall. compile 特殊字符转义 一些现实例子 首发时 ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...
随机推荐
- Pyhon 中文编码问题(字符串前加‘U’)
中文编码问题是用中文的程序员经常头大的问题,在python下也是如此,那么应该怎么理解和解决python的编码问题呢? 我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各 ...
- [JSOI2018]列队(主席树)
跟上次那道列队不一样,但都是九条可怜...(吉老师太强了) 在主席树上统计答案,因为值域只有 \(10^6\) 甚至不用离散化... \(Code\ Below:\) #include <bit ...
- MySQL utilities介绍&出现 No module named utilities
目录 安装 mysqlreplicate mysqlrplcheck mysqlrplshow mysqlrpladmin mysqlfailover mysqldbcompare 详细介绍 mysq ...
- Docker - Docker与Vagrant的区别
Docker Docker - HomePage Wiki - Docker Docker简介 Overview Docker 是一个开源的应用容器引擎,基于 Go 语言并遵从 Apache2.0 协 ...
- vue教程2-07 自定义指令
vue教程2-07 自定义指令 自定义指令: 一.属性: Vue.directive(指令名称,function(参数){ this.el -> 原生DOM元素 }); <div v-re ...
- Solr6.5配置中文分词IKAnalyzer和拼音分词pinyinAnalyzer (二)
之前在 Solr6.5在Centos6上的安装与配置 (一) 一文中介绍了solr6.5的安装.这篇文章主要介绍创建Solr的Core并配置中文IKAnalyzer分词和拼音检索. 一.创建Core: ...
- npm安装第三方库找不到“cl.exe”问题
1.安装第三方库时找不到"cl.exe"的解决方法 安装 本地 remix时 出现错误(npm install remix-ide -g) 原因:remix 依赖的 python库 ...
- Spark程序提交到Yarn集群时所遇异常
Exception 1:当我们将任务提交给Spark Yarn集群时,大多会出现以下异常,如下: 14/08/09 11:45:32 WARN component.AbstractLifeCycle: ...
- centos 7 Mysql5.7 主从复制配置
1.环境 Centos 7 Mysql 5.7 Master 192.168.1.71 Slave01 192.168.1.72 2.分别配置master,slave01 # vi /etc/my. ...
- 使用Docker发布应用
新建spring boot应用demo-docker,添加web依赖 <dependency> <groupId>org.springframework.boot</gr ...