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 ...
随机推荐
- Python3.5学习十八 Python之Web框架 Django
Python之Web框架: 本质:Socket 引用wsgiref创建web框架 根据web框架创建过程优化所得: 分目录管理 模板单独目录 执行不同函数单独存入一个方法py文件 Web框架的两种形式 ...
- linux 的计划任务 定时任务
linux的计划任务,也叫做定时任务 https://www.cnblogs.com/mingforyou/p/3930636.html 名字是crond 查看linux本机的定时任务 crontab ...
- mysql添加外键的4种方式
今天开始复习,在过后的几天里开始在博客上记录一下平时疏忽的知识点,温故而知新 屁话不多--直接上货 创建主表: 班级 CREATE TABLE class(cid INT PRIMARY KEY AU ...
- mysql存储过程双重循环示例
BEGIN ); DECLARE done INT DEFAULT FALSE; DECLARE cursor_rule CURSOR FOR SELECT s.id FROM d_menu_type ...
- BitArray源码解析
BitArray是C# System.Collections内置的集合,用于帮助进行位运算. BitArray的使用示例 // 创建两个大小为 8 的点阵列 BitArray ba1 = new Bi ...
- GITLAB安装笔记
CentOS 7 最小安装后操作 设置时区timedatectl set-timezone Asia/Shanghai 添加 Gitlab 清华源 vi /etc/yum.repos.d/gitlab ...
- android屏幕密度规律及dp px转换
px和dp(sp) 之间转化公式: 1 乘以(dp转px)或者除以(px转dp) scal缩放因子,在上浮0.5f /** * 密度转换像素 * */ public static int dip2p ...
- [转]Android中handler机制的原理
Andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageExchange). 1)Loo ...
- Spring Cloud 使用 FeignClient 启动报错
我们首先来看一下报错信息 Description: Field businessFeignClient in com.ysc.service.BusinessConfigService require ...
- VMware虚拟机屏幕大小只有400,800怎么办如何解决
一,VMware中Linux虚拟机屏幕分辨率调整之前安装修改Linux分辨率命令行 在VMware中安装Linux虚拟机后,屏幕分辨率通常默认设置为800x600,并且不能通过“屏幕分辨率首选项”窗口 ...