python第一个正则表达式

1. import re : python正则表达式模块
2. 第一个正则表达式
re.compile(r'imooc')
pattern.match('imooc python')
示例:
import re
pa = re.compile(r'imooc') #返回一个Pattern类型对象pa
ma = pa.match('imooc python') #返回一个match对象ma
print ma.group() #获得匹配结果
print ma.span() #获得匹配区间
print ma.string #匹配字符串
print ma.re #pattern对象
 
pa = re.compile(r'imooc', re.I)    # I == ignore,忽略大小写地匹配
ma = pa.match('iMoOc python')
print ma.group()
 
pa = re.compile(r'(imooc)') #加入小括号,放到group里
ma = pa.match('imooc python')
print ma.groups()
 
ma = re.match(r'imooc', 'imooc python') #直接匹配
print ma.group()
 
Python正则表达式语法
字符
匹配
.
匹配任意字符(除了\n)
[...]
匹配字符集
\d / \D
匹配数字/非数字
\s / \S
匹配空白/非空白字符
\w / \W
匹配单词字符[a-zA-Z0-9]/非单词字符
单字符匹配示例:
import re
ma = re.match(r'.', 'abc0')
print ma.group()
ma = re.match(r'{[abc]}', '{b}')
print ma.group()
ma = re.match(r'[a-z]', 'a')
print ma.group()
ma = re.match(r'\[[\w]\]', '[a]')
print ma.group()
 
字符
匹配
*
匹配前一个字符0次或无限次
+
匹配前一个字符1次或无限次
?
匹配前一个字符0次或1次
{m} / {m.n}
匹配前一个字符m次到n次
*? / +? / ??
匹配模式变为非贪婪(尽可能少匹配字符)
示例:
# -*- coding: utf-8 -*-
import re
ma = re.match(r'[A-Z][a-z]*', 'Aa')
print ma.group()
ma = re.match(r'[_a-zA-z]+[_\w]*', '_helloWorld01') #匹配一个变量名
print ma.group()
ma = re.match(r'[0-9]?[0-9]', '28') #匹配0到99
print ma.group()
边界匹配
字符
匹配
^
匹配字符串开头
$
匹配字符串结尾
\A / \Z
指定的字符串必须穿现在开头/结尾
示例:
import re
ma = re.match(r'^[\w]{4,10}@163.com$', 'imooc@163.com')
print ma.group()
ma = re.match(r'\Aimooc[\w]*', 'imooc python')
print ma.group()
分组匹配
字符
匹配
|
匹配左右任意一个表达式
(ab)
括号表达式作为一个分组
\<number>
引用编号为num的分组匹配到的字符串
(?P<name>)
分组起一个别名
(?P=name)
引用别名为name的分组匹配字符串
示例:
# -*- coding: utf-8 -*-
import re
ma = re.match(r'[0-9]?\d$|100', '100') #匹配0到100
print ma.group()
ma = re.match(r'[\w]{4,6}@(163|126).com', 'imooc@126.com')
print ma.group()
ma = re.match(r'<([\w]+>)\1', '<book>book>')
print ma.group()
ma = re.match(r'<([\w]+>)[\w]+</\1', '<book>python</book>') #
print ma.group()
ma = re.match(r'<(?P<mark>[\w]+>)[\w]+</(?P=mark)', '<book>python</book>')
print ma.group()
 
re模块的其他方法
1:search(pattern, string, flags=0)
    在一个字符串中查找匹配
2:findall(pattern, string, flags=0)
    找到匹配,返回所有匹配部分的列表
3:sub(pattern, repl, string, count, flags=0)
    将字符串中匹配正则表达式的部分替换为其他值
4:split(pattern, string, maxsplit=0, flags=0)
    根据匹配分割字符串,返回分割字符串组成的列表
示例:
import re
pa = re.compile('<[\w]+>')
ma = re.search(pa, '<a><b><c><d><efg>h<i>')
print ma.group()
pa = re.compile('<[\w]+>')
L = re.findall(pa, '<a><b><c><d><efg>h<i>')
print L
s = re.sub(r'\d+', '2016', 'I was born in 1993')
print s
 
def add1(match):        # repl位函数的sub函数
    val = match.group()
    num = int(val)+1
    return str(num)
s = re.sub(r'\d+', add1, 'I was born in 1993, you are born in 1992')
print s
 
l = re.split(r':| |,', 'imooc:C C++ Java Python,C#')
print l
 
练习
抓取网页中的图片到本地
1:抓取网页
2:抓取图片地址
3:抓取图片内容并把存到本地
 

Python正则表达式 学习笔记的更多相关文章

  1. Python 正则表达式学习笔记

    本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...

  2. 7.Python 正则表达式学习笔记

    本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...

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

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

  4. Python正则表达式学习笔记

    [] 字符类,只要匹配里面的任意字符,都算匹配 . 元字符,可以匹配除换行符之外的所有字符 大小写敏感,但是可以关闭 \d  可以匹配0-9中的任意数字 {3}大括号里面的数字,边上前面一个字符匹配的 ...

  5. 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL

    周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...

  6. Python正则表达式学习摘要及资料

    摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2 ...

  7. Python Click 学习笔记(转)

    原文链接:Python Click 学习笔记 Click 是 Flask 的团队 pallets 开发的优秀开源项目,它为命令行工具的开发封装了大量方法,使开发者只需要专注于功能实现.恰好我最近在开发 ...

  8. JavaScript正则表达式学习笔记(二) - 打怪升级

    本文接上篇,基础部分相对薄弱的同学请移步<JavaScript正则表达式学习笔记(一) - 理论基础>.上文介绍了8种JavaScript正则表达式的属性,本文还会追加介绍几种JavaSc ...

  9. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

随机推荐

  1. html5视频播放解决方案

    关键词:html5  nativeapp webapp mp4 H.264 html5没学习之前总觉的很神秘.近期通过学习和研究html5有点成果,特总结分享给大家.众所周知应用开发分两种:一是原生的 ...

  2. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  3. Java如何格式化24小时格式的时间?

    在Java中,如何格式化24小时格式的时间?? 此示例使用SimpleDateFormat类的sdf.format(date)方法将时间格式化为24小时格式(00:00-24:00). package ...

  4. TPshop标签

    很多cms 中有很多 标签, 商品标签 文章标签  列表标签  几十个标签, 让开发者头疼, 难记,  TPshop开发者考虑到这点, 用了一个万能标签, 开发者非常方便实用 TPshop万能标签只要 ...

  5. 多媒体开发之h264中的sps---sps信息提取之帧率

    ------------------------------author:pkf -----------------------------------------time:2015-8-20 --- ...

  6. 微软office web apps 服务器搭建之在线文档预览

    案例:http://owa.linbsoft.com/op/generate.aspx# 文档地址:http://demo.linbsoft.com/CourseFile/201407/2014070 ...

  7. SSL 证书服务推荐

    最近要用到ssl.故做了一些搜索 1.Let's Encrypt:免费,快捷,支持多域名(不是通配符),三条命令即时签署+导出证书.缺点是暂时只有三个月有效期,到期需续签. 2.StartSSL免费D ...

  8. vue 中view层中方法的使用

    1.使用filters computed:{ }, filters: { filterA: function(value) { return value + 'wh' } }, 2.用法:  {{it ...

  9. Yii2 中cookie的用法(1)

    Yii使用 yii\web\Cookie对象来代表每个cookie,yii\web\Request 和 yii\web\Response 通过名为’cookies’的属性维护一个cookie集合, 前 ...

  10. gridview列超链接的几种设置方法

    <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenera ...