正则表达式:

正则表达式就是描述字符串排列的一套规则。通常被用来检索、替换那些符合某个模式(规则)的文本。

为什么要学习正则表达式呢,因为我们在爬取数据的时候需要根据我们想要获取的内容来进行爬取,而正则表达式就具有这个基本功能。

在python中,一般我们会使用re模块来实现Python正则表达式的功能。

re常用函数解析:

练习:

import re

print(re.match("www","www.baidu.com").span())

print(re.match("www","wwwbaidu.com"))

print(re.match("www","ww.baidu.com"))

print(re.match("www","baidu.wwwcom"))

print(re.match("www","wWw.wwwcom",flags=re.I))

#扫描字符串,返回从头起手位置成功的匹配

练习:

import re

print(re.search("sunck","good man is sunck!sunck is nice"))

print(re.search("sunck","good man is Sunck!sunck is nice"))

print(re.search("sunck","good man is Sunck!sunck is nice",flags=re.I))

import re

print(re.findall("sunck","good man is sunck!Sunck is nice",flags=re.I))

正则表达式元字符:

正则表达式中:

在字符串前加上 r 这个前缀来避免部分疑惑,因为 r 开头的

python字符串是 raw 字符串,所以里面的所有字符都不会被转义 。

一般正则表达式使用反斜杆(\)来转义特殊字符,使其可以匹配字符本身,而不是指定其他特殊的含义。

练习:

import re

print(re.search(".","sunck is a good man 7"))

print(re.findall("[^0-9a-zA-Z_]","sunck is a good man 7"))

print(re.search("\d","sunck is a good man 7"))

print(re.search("[^\d]","sunck is a good man 7"))

print(re.findall("[\W]","sunck is a good man 7 "))

print(re.findall("[\n]","sunck is a good man 7 \n"))

print(re.findall(".","sunck is a good man 7 \n",flags=re.S))

锚字符:

练习:

import re

print(re.search("^sunck","sunck is good man"))

print(re.search("man$","sunck is good man"))

#re.M 多行匹配

print(re.findall("^sunck","sunck is good man\nsunck is good man",re.M))

print(re.search("\Asunck","sunck is good man\nsunck is good man",re.M))

print(re.findall("man$","sunck is good man\nsunck is good man",re.M))

print(re.search("man\Z","sunck is good man\nsunck is good man",re.M))

匹配多个字符

练习:

import re

print(re.findall(r"a?","aaa")) #非贪婪匹配

print(re.findall(r"a*","aaa")) #贪婪匹配

print(re.findall(r".*","aaabaa")) #贪婪匹配

print(re.findall(r"a+","aabaaaa")) #贪婪匹配

print(re.findall(r"a+","aba"))

print(re.findall(r"a{3}","aaaaabaaaaaa"))

特殊:

import re

print(re.findall(r"//*.*/*/", "/* part1 */ /* part */"))

//* 后面的/是转义特殊字符

print(re.findall(r"//*.*?/*/", "/* part1 */ /* part */"))

re模块深入:

str3="sunck is a good man!sunck is a nice man!sunck is a handsome man"

d=re.finditer(r"(sunck)",str3)

while True:

try:

i=next(d)

print(d)

except StopIteration as e:

break

字符串的替换和修改:

str5="sunck is a good good good man!"

print(re.sub(r"(good)","nice",str5))

print(type(re.sub(r"(good)","nice",str5,count=2)))

print(re.subn(r"(good)","nice",str5))

print(type(re.subn(r"(good)","nice",str5)))

分组:

str6="010-53247654"

m=re.match(r"(\d{3})-(\d{8})",str6)

#使用序号获取对应组的信息,group(0)代表原始字符串

print(m.group(0))

print(m.group(1))

print(m.group(2))

print(m.groups())

m1=re.match(r"((\d{3})-(\d{8}))",str6)

print(m1.group(0))

print(m1.group(1))

print(m1.group(2))

print(m1.group(3))

print(m1.groups())

m2=re.match(r"(?P\d{3})-(?P(\d{8}))",str6)

print(m2.group("zz"))

print(m2.group("ss"))

编译:

compile(pattern,flags=0)

pattern:要编译的正则表达式

pat=r"^1(([3578]\d)|(47))\d{8}$"

print(re.match(pat,"13600000000"))

#编译成正则对象

re_telephon=re.compile(pat)

print(re_telephon.match("13600000000"))

#re模块调用 re.match(pattern,string,flags=0)

#re对象调用 re_telephon.match(string)

#re模块调用 re.finditer(pattern,string,flags=0)

#re对象调用 re_telephon.finditer(string)

#re模块调用re.split(pattern,string=,maxsplit=,flags=0)

#re对象调用re_telephon.split(string=,maxsplit=)

#re.sub(pattern=,repl=,string=,count=,flags=)

#re_telephon.sub(repl=,string=,count=)

正则表达式实例练习:

总结:

在学完了正则表达式之后,我们需要多加练习。

有了一定的掌握就可以进入实战状态了。

下一篇:python网络爬虫实战解析。

本文同步分享在 博客“考古学家lx”(CSDN)。

如有侵权,请联系 support@oschina.cn 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

python正则表达式试题,003:Python正则表达式讲解及习题练习相关推荐

  1. 赶紧入手,python面试题之Python如何实现单例模式?

    你现在在找工作吗?还是在观望中呢?快过年了,很多人都会想先存点钱,把年过好再说吧.为了春节,我们加油!哈哈. 回到找工作的话题,遇到心仪的公司了,不可否定的是:最后还是得过了面试那一关.所以,面试前把 ...

  2. python 如何进行内存管理,python面试题之Python是如何进行内存管理的

    python内部使用引用计数,来保持追踪内存中的对象,Python内部记录了对象有多少个引用,即引用计数,当对象被创建时就创建了一个引用计数,当对象不再需要时,这个对象的引用计数为0时,它被垃圾回收. ...

  3. 硬核的 Python 面试题!Python经典面试题总结

    Python面试题总结大全 一.50 道重要的 Python 面试题[问答题+编程题] 1. 说说你用过Python标准库中的哪些模块. 2. `init__`和`__new`方法有什么区别? 3. ...

  4. python面试题之Python支持什么数据类型?

    所属网站分类: 面试经典 > python 作者:外星人入侵 链接:http://www.pythonheidong.com/blog/article/67/ 来源:python黑洞网,专注py ...

  5. python之父去面试-Python面试题之Python的Super方法

    当我第一次见到这个super()的时候,我想说,这是啥啊?为什么Python的super看起来这么奇怪呢? super直接指向父类的实例不就得了么?为什么非得搞两个参数? 而且第一个参数还是自己的类名 ...

  6. python面试题之Python 的特点和优点是什么

    Python 可以作为编程的入门语言,因为他具备以下特质: 1. 解释性 2. 动态特性 3. 面向对象 4. 语法简洁 5. 开源 6. 丰富的社区资源 7 库丰富 本文首发于Python黑洞网,C ...

  7. python面试题之Python如何实现单例模式?

    #使用__metaclass__(元类)的高级python用法 class Singleton2(type): def __init__(cls, name, bases, dict): super( ...

  8. python面试题之Python是如何进行类型转换的

    所属网站分类: 面试经典 > python 作者:外星人入侵 链接: http://www.pythonheidong.com/blog/article/24/ 来源:python黑洞网 www ...

  9. python面试题之python多线程与多进程的区别

    多线程可以共享全局变量,多进程不能 多线程中,所有子线程的进程号相同,多进程中,不同的子进程进程号不同 线程共享内存空间:进程的内存是独立的 同一个进程的线程之间可以直接交流:两个进程想通信,必须通过 ...

最新文章

  1. HTML中div标签的一个简单的使用和介绍
  2. Reddit高赞:机器学习领域「八宗罪」!同行评审变味,盲目崇拜盛行
  3. Errno 256 No more mirrors to try
  4. 【Python】机器学习矩阵运算必学库Numpy首秀!
  5. Ubuntu apt upgrade后黑屏问题
  6. 我端午节又来免费送书了!
  7. 20组免费的用户界面图标,开发者必备
  8. PHP操作视频音频类 FFmpegPHP
  9. 36000+开发者,一周投稿超 23000 篇,谁能笑傲群雄?| 第4周周榜揭晓
  10. ORACLE异常处理及函数
  11. C#人民币金额大写转换
  12. 【2021软件创新实验室暑假集训】总结篇
  13. cmm是什么意思(风量cmm是什么意思)
  14. 使用JWPL处理维基百科数据-使用eclipse
  15. 不用PS,小白也能轻松搞定抠图
  16. 数据结构考研复习 | Fibonacci数列的递归、数组、迭代(循环)实现及其时间复杂度
  17. 迷你播放器--第一阶段(7)--安全攻防第一战--对抗反编译,代码混淆和对抗动态调试
  18. 定义类MyProgram,包含两个属性:一个是private的整型属性data、一个是private的String类型属性str,重写toString,equals
  19. [旭日X3派] 初识篇 - 01
  20. 仿微信查看图片、H5的图片轮播插件PhotoSwipe、SuperSlide

热门文章

  1. Excel数据导入到hbase实战
  2. @import 错误用法
  3. 华为鸿蒙与鸿蒙网,华为鸿蒙专题研究报告:鸿蒙生态加速,国产软件迎长期机遇...
  4. 列表含有子列表展开成一个列表
  5. Android中内存泄漏超级精炼详解
  6. java | (三十一)MyBatis(1)配置、映射、缓存
  7. contactform7 ajax,WordPress询盘插件 – Contact Form 7
  8. 计算机字符格式化集体备课教案,有序备课:集体备课中不要忘记个性化备课
  9. 香港五个遊客不常到的本地拍攝熱門地點
  10. 「镁客早报」三星折叠屏手机中国区发布会临时取消;特斯拉在地库中突然自燃... 1