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-模式匹配的更多相关文章

  1. python中正则表达式与模式匹配

    一.前言 在之前找工作过程中,面试时经常被问到会不会python,懂不懂正则表达式.心里想:软件的东西和芯片设计有什么关系?咱也不知道因为啥用这个,咱也不敢问啊!在网上搜索到了一篇关于脚本在ASIC领 ...

  2. Python正则表达式详解

    我用双手成就你的梦想 python正则表达式 ^ 匹配开始 $ 匹配行尾 . 匹配出换行符以外的任何单个字符,使用-m选项允许其匹配换行符也是如此 [...] 匹配括号内任何当个字符(也有或的意思) ...

  3. 比较详细Python正则表达式操作指南(re使用)

    比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...

  4. Python天天美味(15) - Python正则表达式操作指南(re使用)(转)

    http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...

  5. python 正则表达式 学习笔记(不断补充ing)

    本文参考了以下博客,感谢众位大神的分享! http://www.oschina.net/question/12_9507 和 http://www.crifan.com/python_re_sub_d ...

  6. Python 正则表达式(字符)详解

    Python正则表达式 - 简介 ​    其实正则表达式这种技术,源于一个很简单的问题:  如何通过变成使得计算机具有在文本中检索某种模式的能力? ​     而正则表达式为通过编程实现高级的文本模 ...

  7. python 正则表达式Re

    Python正则表达式指南这篇文章很好,推荐阅读. 本文则是简单记录下我自己学习Re的笔记, 环境是python3.5. 1.简单的Re语法 ^ 匹配字符串开始位置. $ 匹配字符串结束位置. \b ...

  8. python大法好——Python 正则表达式

    Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. r ...

  9. python正则表达式模块re:正则表达式常用字符、常用可选标志位、group与groups、match、search、sub、split,findall、compile、特殊字符转义

    本文内容: 正则表达式常用字符. 常用可选标志位. group与groups. match. search. sub. split findall. compile 特殊字符转义 一些现实例子 首发时 ...

  10. python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL

    python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...

随机推荐

  1. Spring Boot切换为APR模式

    Spring Boot内置了tomcat容器,直接运行Application就可以启动web服务器. 在tomcat中提供了三种方式:BIO.NIO.APR. BIO tomcat7以下的版本都是BI ...

  2. DevOps - CI - Jenkins

    Jenkins 开源软件项目,其前身为Hudson,旨在提供一个基于Java开发的开放易用的持续集成工具,用于监控持续重复的工作. 主要用于自动而持续地构建/测试软件项目:监控外部调用执行的工作. 官 ...

  3. 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件时,常常提示“有未能满足的依赖关系“,解决方法

    很早之前在ubuntu安装软件时遇到的问题,今天打开ubuntu看到了,总结如下: 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件常常提示“有 ...

  4. 怎么把excel表格内的数据导入数据库?

    第一种方法: 思路:想要把excel表格内的数据直接导入数据库不是那么容易,可以把excel表格另存为.csv格式的文档(特点:内容以逗号分割):然后通过一系列的文档操作函数处理成为一个二维数组,然后 ...

  5. Liferay-Activiti 功能介绍 (新版Liferay7基本特性)

    一句话简介 Liferay是世界领先的开源企业门户(也可作为综合门户),是最强大(没有之一)的JAVA开源门户,在Gartner和Forrester和评价非常高,近几年已经超越了微软门户Sharepo ...

  6. 基于cytoscape.js 、 d3.js实现的关系图谱初级版本

    前面的文章已经介绍了cytoscape.js . d3.js的安装及简单demo,现在展示从html页面转移到vue项目下的最初版的demo 效果图: 代码如下: <template> & ...

  7. python 数据库连接及操作

    Python DB-API使用流程: 引入API模块. 获取与数据库的连接. 执行SQL语句和存储过程. 关闭数据库连接. def mysql_dbtest(): config = { 'host': ...

  8. 利用django-simple-captcha生成验证码

    参考文档 http://django-simple-captcha.readthedocs.io/en/latest/ django支持1.7+ 1.安装 pip install django-sim ...

  9. django安装与环境调配

    91.查看当前已有虚拟环境 2.进入虚拟环境 3.退出当前虚拟环境 4.创建python隔离环境前先可用此先查看已经拥有什么版本的python 5.创建一个名为first的python隔离环境 6.安 ...

  10. Gen对于数组Array的处理

    举个例子,如下: public void t() { String[][] a = new String[][] { { "x", "y" } }; Strin ...