Python模块03/re模块

内容大纲

re模块(正则表达式)

1.re模块(正则表达式)

import re
s = "meet_宝元_meet"
print(re.findall("meet",s))
从字符串中全部查找内容,返回一个列表 s = "meet_宝元_meet123"
print(re.findall("\w",s))
查找数字,字母(中文),下划线 # s = "meet_宝元_meet123!@#"
# print(re.findall("\W",s))
# 查找非数字,字母(中文),下划线 # s = "meet_ 宝元_ me et\t \n"
# print(re.findall("\s",s))
# 查找任意空格,换行符,制表符 # s = "meet_ 宝元_ me et\t \n"
# print(re.findall("\S",s))
# 查找非任意空格,换行符,制表符 s = "meet_ 宝元_123me et\t \n"
print(re.findall("\d",s))
查找数字 print(re.findall("\D",s))
查找非数字 # s = "meet宝元_123meet\t \n"
# print(re.findall("\Ameet",s))
# print(re.findall("^meet",s))
# 查找是否以什么开头的内容 # s = "meet宝元_123meet"
# print(re.findall("t\Z",s))
# print(re.findall("t$",s))
# 查找是否以什么结尾的内容 # s = "meet宝元_123meet \n \t \n"
# print(re.findall("\n",s))
# 查找换行符 # print(re.findall("\t",s))
# 查找制表符 # s = "m\net宝元_123maet \n \t "
# print(re.findall("m.e",s))
# .只能匹配任意一个内容(非换行符) # s = "m\net宝元_123maet \n \t "
# print(re.findall("m.e",s,re.DOTALL))
# .只能匹配任意一个内容 # s = "meet宝元_1A-23maet"
# print(re.findall("[a-z]",s)) # 小写的a,z
# print(re.findall("[A-Z]",s)) # 大写的A,Z
# print(re.findall("[A-Za-z]",s)) # 大写和小写的a,z A,Z
# print(re.findall("[a-z0-9]",s)) # 小写的a,z 数字 0,9 # s = "meet宝元_1A-23maet"
# print(re.findall("[^0-9]",s)) # [^0-9] 查找非0-9的内容 s = "mmmmmm"
print(re.findall("m*",s)) # * 匹配 0个多个 [贪婪匹配] s = "meet_asdf_msss_mmns_aaam" # + 匹配1个或多个 [贪婪匹配]
print(re.findall("me+",s)) # me,mee,meee,meeeee, s = "meet_asdf_msss_mmns_aaam" # ? 匹配 0个或1个 [非贪婪匹配]
print(re.findall("m?",s)) # s = "meet_asdf_msss_mmns_aaam" # s{3} s重复3次 == sss
# print(re.findall("s{3}",s)) # s = "meet_assdf_msss_mmns_aaam"
# print(re.findall("s{1,3}",s)) # s{1,3} s ss sss
# 指定最少多少次,最多多少次 # a|b # 或
# s = "meet_assdf_msss_mmns_aaam"
# print(re.findall("m|s",s)) # m或者s # s = "meet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssstt"
# print(re.findall("m(.+)t",s)) # s = "meet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssstt"
# print(re.findall("m(?:..?)t",s)) # s = 'alex_sb ale123_sb wu12sir_sb wusir_sb ritian_sb 的 alex wusir '
# print(re.findall("\w+_sb",s))
# print(re.findall("[a-z]+_sb",s)) s = '_sb alex 123_sb wu12sir_sb wusir_sb ritian_sb 的 x wusir '
print(re.search("ale",s).group())
search 找到1个后就停止查找了,从字符串中进行查找.找到后返回的是一个对象,查看元素.group() s = '_sb alex 123_sb wu12sir_sb wusir_sb ritian_sb 的 x wusir '
print(re.match("ale",s).group())
match 找到1个后就停止查找了,只从字符串的开头查找.找到后返回的是一个对象,查看元素.group() # s = '_sb alex,123:sb;wu12sir#sb*wusir!sb ritian_sb 的 x wusir '
# print(re.split("[#,:!*]",s))
# 分割 # 替换
# print(re.sub("barry","太亮",'barry是最好的讲师,barry就是一个普通老师,请不要将barry当男神对待。')) # compile # 定义匹配规则
# obj = re.compile("\w")
# print(obj.findall("meet_宝元_常鑫垃圾")) # re.findall("\w","meet_宝元_常鑫大煎饼") # finditer # 返回是一个迭代器的地址
# g = re.finditer("\w","常鑫垃圾")
# print(next(g).group())
# for i in g:
# print(i.group()) # print(re.findall("常(.*?)娃","常鑫垃圾_井盖_烧饼吃娃娃_自行车_葫芦爷爷救娃娃"))
# print(re.findall("常(.*?)娃","常鑫垃圾_井盖_烧饼吃娃娃_自行车_葫芦爷爷救娃娃")) # print(re.search("(?P<tag_name>\w+)\w+\w+","h1hellh1"))
# print(re.search("(?P<aaa>\w+)dfa","asbsadfasdfa").group("aaa"))
# print(re.search("(?P<cx>\w+)dfa","asbsadfasdfa").group()) # 1 "1-2*(60+(-40.35/5)-(-4*3))"
# 1.1 匹配所有的整数 # s = "1-2*(60+(-40.35/5)-(-4*3))"
# print(re.findall("\d+",s)) # 匹配所有的数字(包含小数)
# print(re.findall("\d+\.\d+|\d+",s)) # 匹配所有的数字(包含小数包含负号)
# print(re.findall("-?\d+\.\d+|-?\d+",s)) # s = "http://blog.csdn.net/make164492212/article/details/51656638"
# print(re.findall("h.*2/",s)) s1 = '''
时间就是1995-04-27,2005-04-27
1999-04-27 老男孩教育创始人
老男孩老师 alex 1980-04-27:1980-04-27
2018-12-08
'''
# print(re.findall("\d+-\d+-\d+",s1)) # 匹配qq号:腾讯从10000开始 # qq = input("请输入QQ号:")
# print(re.findall("[1-9][0-9]{4,9}",qq)) s1 = '''
<div id="cnblogs_post_body" class="blogpost-body"><h3><span style="font-family: 楷体;">python基础篇</span></h3>
<p><span style="font-family: 楷体;">&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6847032.html" target="_blank">python 基础知识</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6627631.html" target="_blank">python 初始python</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/articles/7087609.html" target="_blank">python 字符编码</a></strong></strong></span></p>
<p><span style="font-family: 楷体;"><strong><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/6752157.html" target="_blank">python 类型及变量</a></strong></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6847663.html" target="_blank">python 字符串详解</a></strong></span></p>
<p><span style="font-family: 楷体;">&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6850347.html" target="_blank">python 列表详解</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6850496.html" target="_blank">python 数字元祖</a></strong></span></p>
<p><span style="font-family: 楷体;">&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6851820.html" target="_blank">python 字典详解</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<strong><a href="http://www.cnblogs.com/guobaoyuan/p/6852131.html" target="_blank">python 集合详解</a></strong></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/7087614.html" target="_blank">python 数据类型</a>&nbsp;</strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/6752169.html" target="_blank">python文件操作</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/p/8149209.html" target="_blank">python 闭包</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/6705714.html" target="_blank">python 函数详解</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/7087616.html" target="_blank">python 函数、装饰器、内置函数</a></strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/7087629.html" target="_blank">python 迭代器 生成器</a>&nbsp;&nbsp;</strong></span></p>
<p><span style="font-family: 楷体;"><strong>&nbsp; &nbsp;<a href="http://www.cnblogs.com/guobaoyuan/articles/6757215.html" target="_blank">python匿名函数、内置函数</a></strong></span></p>
</div>
''' # print(re.findall("<span(.*?)>",s1))
# print(re.findall('<a href="(.*?)"',s1))

2.今日总结

# re -- 正则表达式
import re
# re.findall()
# re.search()
# re.match()
# re.sub()
# re.split()
# re.finditer() # \w
# \d
# ^
# $
# m()t m(?:)t
# [] [^]
# {}
# | 或者
# \. == 转义成普通的. # * 0或多
# + 1或多
# ? 0或1
# *? 限制*
# .* 一个任意元素重复出现0次或多次 # m(?P<名字>\w+)t group("名字")

Python模块03/re模块的更多相关文章

  1. Python进阶03 模块

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们之前看到了函数和对象.从本质上来说,它们都是为了更好的组织已经有的程序,以方便 ...

  2. Python模块之常用模块,反射以及正则表达式

    常用模块  1. OS模块 用于提供系统级别的操作,系统目录,文件,路径,环境变量等 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("di ...

  3. Python自动化之常用模块

    1 time和datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理 ...

  4. Python学习笔记-常用模块

    1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...

  5. Python处理时间 time && datetime 模块

    Python处理时间 time  &&  datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...

  6. Python开发【第一篇】Python基础之自定义模块和内置模块

    为什么要有模块,将代码归类.模块,用一砣代码实现了某个功能的代码集合. Python中叫模块,其他语言叫类库. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代 ...

  7. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  8. python笔记之常用模块用法分析

    python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...

  9. Python学习之路——模块

    一.模块: 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...

随机推荐

  1. ArchLinux的安装

    ArichLinux安装教程 Arch Linux 于 2002 年发布,由 Aaron Grifin 领头,是当下最热门的 Linux 发行版之一.从设计上说,Arch Linux 试图给用户提供简 ...

  2. Charles抓包1-Charles安装汉化(附正版注册码)

    目录 1.下载&&安装 2.汉化 1.下载&&安装 charles官网 charles下载 下载后直接安装即可. 2.汉化 下载提供的汉化包charles.jar(加群 ...

  3. idea出现 淇℃伅 乱码

    问题:我是idea出现 淇℃伅 乱码, 解决:修改 tomcat 下的 logging.properties这个文件 为 GBK 就好了. 参考:https://blog.csdn.net/weixi ...

  4. MySQL 性能优化之慢查询

    性能优化的思路 首先需要使用慢查询功能,去获取所有查询时间比较长的SQL语句 其次使用explain命令去查询由问题的SQL的执行计划(脑补链接:点我直达1,点我直达2) 最后可以使用show pro ...

  5. git 提交流程

    Git提交流程: 1. Menu remote > (拉取)fetch 2. 重新扫描(rescan) 3. 缓存改动(stage change) 4. 写注释后提交(commit) 5. Me ...

  6. 安卓开发,Service 服务

    Service 服务 是一种应用组件,可长时间后台运行,不提供用户界面.如音乐播放器/下载程序.不能自己运行. 使用Service的方式: (一)startService(): 调用者和服务之间没有联 ...

  7. 微信小程序-页面栈

    在小程序中以栈的形式维护了当前的所有页面 当发生路由切换的时候,页面栈的表现如下: 初始化:新页面入栈 打开新页面:新页面入栈(调用 API wx.navigateTo 或使用组件 <navig ...

  8. js清除所有的空格

    /** * 清除所有的空格 * @returns {*} */ String.prototype.removeSpace = function () { var str = this.replaceA ...

  9. redis基础二----操作List类型

    1.lpush的使用方法 2.rpsuh的使用方法 3.删除元素 lrem中2值的是删除2个集合中的“b”元素 4. 通过上面的分析,redis中的list比较类型java的qunue队列

  10. Python3-apscheduler模块-定时调度

    from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler from apschedule ...