import re

1 查找第一个匹配串

s = 'i love python very much'
pat = 'python'
r = re.search(pat,s)
print(r.span()) #(7,13)

2 查找所有1

s = '山东省潍坊市青州第1中学高三1班'
pat = '1'
r = re.finditer(pat,s)
for i in r:print(i)# <re.Match object; span=(9, 10), match='1'>
# <re.Match object; span=(14, 15), match='1'>

3 \d匹配数字[0-9]

s = '一共20行代码运行时间13.59s'
pat = r'\d+' # +表示匹配数字(\d表示数字的通用字符)1次或多次
r = re.findall(pat,s)
print(r)
# ['20', '13', '59']

我们想保留13.59而不是分开,请看4

4 ?表示前一个字符匹配0或1次

s = '一共20行代码运行时间13.59s'
pat = r'\d+\.?\d+' # ?表示匹配小数点(\.)0次或1次
r = re.findall(pat,s)
print(r)
# ['20', '13.59']

5 ^匹配字符串的开头

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'^[emrt]' # 查找以
r = re.findall(pat,s)
print(r)
# [],因为字符串的开头是字符`T`,不在emrt匹配范围内,所以返回为空

6 re.I 忽略大小写

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'^[emrt]' # 查找以
r = re.compile(pat,re.I).search(s)
print(r)
# <re.Match object; span=(0, 1), match='T'> 表明字符串的开头在匹配列表中

7 使用正则提取单词
这是不准确版本,请参看第9个

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'\s[a-zA-Z]+'
r = re.findall(pat,s)
print(r) #[' module', ' provides', ' regular', ' expression', ' matching', ' operations', ' similar', ' to', ' those', ' found', ' in', ' Perl']

8 只捕获单词,去掉空格
使用()捕获,这是不准确版本,请参看第9个

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'\s([a-zA-Z]+)'
r = re.findall(pat,s)
print(r) #['module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']

9 补充上第一个单词
上面第8,看到提取单词中未包括第一个单词,使用?表示前面字符出现0次或1次,但是此字符还有表示贪心或非贪心匹配含义,使用时要谨慎。

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'\s?([a-zA-Z]+)'
r = re.findall(pat,s)
print(r) #['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']

10 使用split函数直接分割单词
使用以上方法分割单词,不是简洁的,仅仅为了演示。分割单词最简单还是使用split函数。

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'\s+'
r = re.split(pat,s)
print(r) # ['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']

11 提取以m或t开头的单词,忽略大小写
下面出现的结果不是我们想要的,原因出在 ?上!

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'\s?([mt][a-zA-Z]*)' # 查找以
r = re.findall(pat,s)
print(r) # ['module', 'matching', 'tions', 'milar', 'to', 'those']

12 使用^查找字符串开头的单词
综合11和12得到所有以m或t开头的单词

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'^([mt][a-zA-Z]*)\s' # 查找以
r = re.compile(pat,re.I).findall(s)
print(r) # ['This']

13 先分割,再查找满足要求的单词
使用match表示是否匹配

s = 'This module provides regular expression matching operations similar to those found in Perl'
pat = r'\s+'
r = re.split(pat,s)
res = [i for i in r if re.match(r'[mMtT]',i)]
print(res) # ['This', 'module', 'matching', 'to', 'those']

14 贪心匹配
尽可能多的匹配字符

content='<h>ddedadsad</h><div>graph</div>bb<div>math</div>cc'
pat=re.compile(r"<div>(.*)</div>")  #贪婪模式
m=pat.findall(content)
print(m) # ['graph</div>bb<div>math']

15 非贪心匹配
与14相比,仅仅多了一个问号(?),得到结果完全不同。

content='<h>ddedadsad</h><div>graph</div>bb<div>math</div>cc'
pat=re.compile(r"<div>(.*?)</div>")  #贪婪模式
m=pat.findall(content)
print(m) # ['graph', 'math']

与14比较可知,贪心匹配和非贪心匹配的区别,后者是字符串匹配后立即返回,见好就收。

16 含有多种分割符
使用split函数

content = 'graph math,,english;chemistry' # 这种
pat=re.compile(r"[\s\,\;]+")  #贪婪模式
m=pat.split(content)
print(m) # ['graph', 'math', 'english', 'chemistry']

17 替换匹配的子串
sub函数实现对匹配子串的替换

content="hello 12345, hello 456321"
pat=re.compile(r'\d+') #要替换的部分
m=pat.sub("666",content)
print(m) # hello 666, hello 666

18 爬取百度首页标题

import re
from urllib import request#爬虫爬取百度首页内容
data=request.urlopen("http://www.baidu.com/").read().decode()#分析网页,确定正则表达式
pat=r'<title>(.*?)</title>'result=re.search(pat,data)
print(result) <re.Match object; span=(1358, 1382), match='<title>百度一下,你就知道</title>'>result.group() # 百度一下,你就知道

19 常用元字符总结

. 匹配任意字符
^ 匹配字符串始位置
$ 匹配字符串中结束的位置
* 前面的原子重复0次1次多次
? 前面的原子重复一次或者0次
+ 前面的原子重复一次或多次
{n} 前面的原子出现了 n 次
{n,} 前面的原子至少出现 n 次
{n,m} 前面的原子出现次数介于 n-m 之间
( ) 分组,需要输出的部分

20 常用通用字符总结

\s  匹配空白字符
\w  匹配任意字母/数字/下划线
\W  和小写 w 相反,匹配任意字母/数字/下划线以外的字符
\d  匹配十进制数字
\D  匹配除了十进制数以外的值
[0-9]  匹配一个0-9之间的数字
[a-z]  匹配小写英文字母
[A-Z]  匹配大写英文字母

Python正则表达式,简单20个用例学习相关推荐

  1. python 正则表达式(Regular Expression)基础学习笔记

    python 正则表达式(Regular Expression) 正则表达式基础 search():只返回第一个匹配的字符串 findall():将搜寻结果以列表方式返回 import re # 导入 ...

  2. python超简单趣味编程100例_python趣味编程100例

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 001 # -*- coding: cp936 -*- 002 from math import acos,sqrt 003 #第一章----最简单的问题 ...

  3. python正则表达式提取电话号码区号_Python学习笔模式匹配与正则表达式之电话号码和Email地址提取程序...

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  4. python正则表达式 简单的手机号码格式的验证

    import re#手机号的匹配 phone = re.compile('^(13(7|8|9|6|5|4)|17(0|8|3|7)|18(2|3|6|7|9)|15(3|5|6|7|8|9))\d{ ...

  5. Python正则表达式简单说明(菜鸟教程里面的说明)

    摘自菜鸟教程 ::大家可以去看一下 挺有用的 正则表达式--可选标志含义 re.match()::匹配开头 re.search()::全部匹配 源自:http://www.runoob.com/pyt ...

  6. python正则表达式需要模块_使用Python正则表达式模块,让操作更加简单

    处理文本数据的一个主要任务就是创建许多以文本为基础的特性. 人们可能想要在文本中找出特定格式的内容,比如找出存在于文本中的电子邮件,或者大型文本中的电话号码. 虽然想要实现上述功能听起来很繁琐,但是如 ...

  7. 正则表达式 - Python 正则表达式 学习笔记 最全整理

    正则表达式 regular expression,用于匹配字符串中字符组成的方法,"用有限的符号表达无限的序列". JavaScript 正则表达式语法如下: /xxx/gi 两条 ...

  8. Python 正则表达式学习----flags 编译标志位

    Python 正则表达式学习--flags 编译标志位 关于正则表达式更多的内容,推荐https://blog.csdn.net/longerzone/article/details/24303161 ...

  9. python爬虫正则表达式实例-3.Python爬虫入门_正则表达式(简单例子)

    1 #2019-11-23 2 importrequests3 importtime4 import re #Python正则表达式库 5 6 if __name__=='__main__':7 #海 ...

最新文章

  1. 关于webpack的使用
  2. 心电图前波过多_心电图写着:T波倒置,就是心肌缺血吗?医生:不能如此草率...
  3. CSS3 Flexbox轻松实现元素的水平居中和垂直居中
  4. java面试要点---Spring体系知识点复习,IOC,AOP---随时更新
  5. IBM X3650 M4 服务器
  6. 【论文写作】毕业论文写作套路之正文(2)
  7. Reflector使用手记
  8. 你还在全量加载iView么?试试手动配一个吧
  9. php 5.5.9安装,php5.5.9+apache2.4.7 编译安装
  10. java中的前加加 和 后加加
  11. Hadoop原理和特性
  12. 最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
  13. Android动态获取权限(读写、获取手机状态、定位)
  14. Android Pixel手机Notification小图标显示白方块问题
  15. ecshop模板教程——类似淘宝滚屏漂浮返回
  16. 数据结构之——关键路径
  17. java简单爬虫实现打印小说章节至控制台
  18. 因果推理(三):关联和因果在因果图中的流动
  19. 009 简单的渗透测试流程
  20. 给ROCK64安装opencv3(Ubuntu,Debian)

热门文章

  1. 初学__Python——Python数据类型之字符串
  2. 修建道路 贪心,思维(女赛)
  3. JavaWeb中如何通过Request对象获取客户端IP地址
  4. book mac pro怎么重装系统_Macbook Pro怎么重装系统
  5. r语言各形状编号_R语言入门第八讲:编码分类变量(factor)
  6. python常用编译器和解释器的区别_Python常用编译器原理及特点解析
  7. python清除缓存的命令_python – 重启django服务器时清除缓存的最佳位置
  8. 无锡易保Java面试笔试_易保面试题 - willim - BlogJava
  9. mysql锁机制为何设计如此复杂_再谈mysql锁机制及原理—锁的诠释
  10. mysql存储引擎简书_MySQL存储引擎详解