re.S,使 '.'  匹配换行在内的所有字符

  1. >>> pattern=r'ghostwu.com'
  2. >>> import re
  3. >>> re.findall( pattern, 'ghostwuacom' )
  4. ['ghostwuacom']
  5. >>> re.findall( pattern, 'ghostwubcom' )
  6. ['ghostwubcom']
  7. >>> re.findall( pattern, 'ghostwu.com' )
  8. ['ghostwu.com']
  9. >>> re.findall( pattern, 'ghostwu\ncom' )
  10. []
  11. >>> re.findall( pattern, 'ghostwu\ncom', re.S )
  12. ['ghostwu\ncom']
  13. >>>

re.M,多行匹配,主要影响( ^和$ )

  1. >>> str="""
  2. ... hi,ghostwu,how are you
  3. ... ghostwu: my name is ghostwu,how are you
  4. ... ghostwu: nice to meet you
  5. ... hello ghostwu
  6. ... """
  7. >>> pattern = r"^ghostwu"
  8. >>> re.findall( pattern, str )
  9. []
  10. >>> re.findall( pattern, str, re.M )
  11. ['ghostwu', 'ghostwu']
  12. >>>

当正则有多行的时候,可以开启verbose模式re.X

  1. >>> pattern=r"""
  2. ... \d{3,4}
  3. ... -?
  4. ... \d{8}
  5. ... """
  6. >>> str="020-88888888"
  7. >>> re.findall( pattern, str )
  8. []
  9. >>> re.findall( pattern, str, re.X )
  10. ['020-88888888']
  11. >>>

():分组与|  的使用,  假如我们要匹配一个.com,.cn,.net结尾的email

  1. >>> pattern=r"\w+@\w+(.com|.cn|.net)"
  2. >>> email="abc@qq.com">>> re.match( pattern, email )
  3. <_sre.SRE_Match object at 0x7f2b74481828>
  4. >>> re.match( pattern, 'abc@qq.cn' )
  5. <_sre.SRE_Match object at 0x7f2b744818a0>
  6. >>> re.match( pattern, 'abc@qq.net' )
  7. <_sre.SRE_Match object at 0x7f2b74481828>
  8. >>> re.match( pattern, 'abc@qq.io' )
  9. >>>

匹配超链接

  1. >>> html="""
  2. ... <a href="http://www.baidu.com">百度</a>
  3. ... <a href="index.html">首页</a>
  4. ... <p>这是一段说明</p>
  5. ... <a href="http://www.taobao.com">淘宝</a>
  6. ... """
  7. >>> re=r"href=\"(.+?)\""
  8. >>> pattern=r"href=\"(.+?)\""
  9. >>> re
  10. 'href=\\"(.+?)\\"'
  11. >>> import re
  12. >>> re.findall( pattern, html )
  13. ['http://www.baidu.com', 'index.html', 'http://www.taobao.com']
  14. >>>

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. Python3.5学习十八 Python之Web框架 Django

    Python之Web框架: 本质:Socket 引用wsgiref创建web框架 根据web框架创建过程优化所得: 分目录管理 模板单独目录 执行不同函数单独存入一个方法py文件 Web框架的两种形式 ...

  2. linux 的计划任务 定时任务

    linux的计划任务,也叫做定时任务 https://www.cnblogs.com/mingforyou/p/3930636.html 名字是crond 查看linux本机的定时任务 crontab ...

  3. mysql添加外键的4种方式

    今天开始复习,在过后的几天里开始在博客上记录一下平时疏忽的知识点,温故而知新 屁话不多--直接上货 创建主表: 班级 CREATE TABLE class(cid INT PRIMARY KEY AU ...

  4. mysql存储过程双重循环示例

    BEGIN ); DECLARE done INT DEFAULT FALSE; DECLARE cursor_rule CURSOR FOR SELECT s.id FROM d_menu_type ...

  5. BitArray源码解析

    BitArray是C# System.Collections内置的集合,用于帮助进行位运算. BitArray的使用示例 // 创建两个大小为 8 的点阵列 BitArray ba1 = new Bi ...

  6. GITLAB安装笔记

    CentOS 7 最小安装后操作 设置时区timedatectl set-timezone Asia/Shanghai 添加 Gitlab 清华源 vi /etc/yum.repos.d/gitlab ...

  7. android屏幕密度规律及dp px转换

    px和dp(sp) 之间转化公式: 1  乘以(dp转px)或者除以(px转dp) scal缩放因子,在上浮0.5f /** * 密度转换像素 * */ public static int dip2p ...

  8. [转]Android中handler机制的原理

    Andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageExchange). 1)Loo ...

  9. Spring Cloud 使用 FeignClient 启动报错

    我们首先来看一下报错信息 Description: Field businessFeignClient in com.ysc.service.BusinessConfigService require ...

  10. VMware虚拟机屏幕大小只有400,800怎么办如何解决

    一,VMware中Linux虚拟机屏幕分辨率调整之前安装修改Linux分辨率命令行 在VMware中安装Linux虚拟机后,屏幕分辨率通常默认设置为800x600,并且不能通过“屏幕分辨率首选项”窗口 ...