Python模块03/re模块

内容大纲

re模块(正则表达式)

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

  1. import re
  2. s = "meet_宝元_meet"
  3. print(re.findall("meet",s))
  4. 从字符串中全部查找内容,返回一个列表
  5. s = "meet_宝元_meet123"
  6. print(re.findall("\w",s))
  7. 查找数字,字母(中文),下划线
  8. # s = "meet_宝元_meet123!@#"
  9. # print(re.findall("\W",s))
  10. # 查找非数字,字母(中文),下划线
  11. # s = "meet_ 宝元_ me et\t \n"
  12. # print(re.findall("\s",s))
  13. # 查找任意空格,换行符,制表符
  14. # s = "meet_ 宝元_ me et\t \n"
  15. # print(re.findall("\S",s))
  16. # 查找非任意空格,换行符,制表符
  17. s = "meet_ 宝元_123me et\t \n"
  18. print(re.findall("\d",s))
  19. 查找数字
  20. print(re.findall("\D",s))
  21. 查找非数字
  22. # s = "meet宝元_123meet\t \n"
  23. # print(re.findall("\Ameet",s))
  24. # print(re.findall("^meet",s))
  25. # 查找是否以什么开头的内容
  26. # s = "meet宝元_123meet"
  27. # print(re.findall("t\Z",s))
  28. # print(re.findall("t$",s))
  29. # 查找是否以什么结尾的内容
  30. # s = "meet宝元_123meet \n \t \n"
  31. # print(re.findall("\n",s))
  32. # 查找换行符
  33. # print(re.findall("\t",s))
  34. # 查找制表符
  35. # s = "m\net宝元_123maet \n \t "
  36. # print(re.findall("m.e",s))
  37. # .只能匹配任意一个内容(非换行符)
  38. # s = "m\net宝元_123maet \n \t "
  39. # print(re.findall("m.e",s,re.DOTALL))
  40. # .只能匹配任意一个内容
  41. # s = "meet宝元_1A-23maet"
  42. # print(re.findall("[a-z]",s)) # 小写的a,z
  43. # print(re.findall("[A-Z]",s)) # 大写的A,Z
  44. # print(re.findall("[A-Za-z]",s)) # 大写和小写的a,z A,Z
  45. # print(re.findall("[a-z0-9]",s)) # 小写的a,z 数字 0,9
  46. # s = "meet宝元_1A-23maet"
  47. # print(re.findall("[^0-9]",s)) # [^0-9] 查找非0-9的内容
  48. s = "mmmmmm"
  49. print(re.findall("m*",s)) # * 匹配 0个多个 [贪婪匹配]
  50. s = "meet_asdf_msss_mmns_aaam" # + 匹配1个或多个 [贪婪匹配]
  51. print(re.findall("me+",s)) # me,mee,meee,meeeee,
  52. s = "meet_asdf_msss_mmns_aaam" # ? 匹配 0个或1个 [非贪婪匹配]
  53. print(re.findall("m?",s))
  54. # s = "meet_asdf_msss_mmns_aaam" # s{3} s重复3次 == sss
  55. # print(re.findall("s{3}",s))
  56. # s = "meet_assdf_msss_mmns_aaam"
  57. # print(re.findall("s{1,3}",s)) # s{1,3} s ss sss
  58. # 指定最少多少次,最多多少次
  59. # a|b # 或
  60. # s = "meet_assdf_msss_mmns_aaam"
  61. # print(re.findall("m|s",s)) # m或者s
  62. # s = "meet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssstt"
  63. # print(re.findall("m(.+)t",s))
  64. # s = "meet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssstt"
  65. # print(re.findall("m(?:..?)t",s))
  66. # s = 'alex_sb ale123_sb wu12sir_sb wusir_sb ritian_sb 的 alex wusir '
  67. # print(re.findall("\w+_sb",s))
  68. # print(re.findall("[a-z]+_sb",s))
  69. s = '_sb alex 123_sb wu12sir_sb wusir_sb ritian_sb 的 x wusir '
  70. print(re.search("ale",s).group())
  71. search 找到1个后就停止查找了,从字符串中进行查找.找到后返回的是一个对象,查看元素.group()
  72. s = '_sb alex 123_sb wu12sir_sb wusir_sb ritian_sb 的 x wusir '
  73. print(re.match("ale",s).group())
  74. match 找到1个后就停止查找了,只从字符串的开头查找.找到后返回的是一个对象,查看元素.group()
  75. # s = '_sb alex,123:sb;wu12sir#sb*wusir!sb ritian_sb 的 x wusir '
  76. # print(re.split("[#,:!*]",s))
  77. # 分割
  78. # 替换
  79. # print(re.sub("barry","太亮",'barry是最好的讲师,barry就是一个普通老师,请不要将barry当男神对待。'))
  80. # compile # 定义匹配规则
  81. # obj = re.compile("\w")
  82. # print(obj.findall("meet_宝元_常鑫垃圾"))
  83. # re.findall("\w","meet_宝元_常鑫大煎饼")
  84. # finditer # 返回是一个迭代器的地址
  85. # g = re.finditer("\w","常鑫垃圾")
  86. # print(next(g).group())
  87. # for i in g:
  88. # print(i.group())
  89. # print(re.findall("常(.*?)娃","常鑫垃圾_井盖_烧饼吃娃娃_自行车_葫芦爷爷救娃娃"))
  90. # print(re.findall("常(.*?)娃","常鑫垃圾_井盖_烧饼吃娃娃_自行车_葫芦爷爷救娃娃"))
  91. # print(re.search("(?P<tag_name>\w+)\w+\w+","h1hellh1"))
  92. # print(re.search("(?P<aaa>\w+)dfa","asbsadfasdfa").group("aaa"))
  93. # print(re.search("(?P<cx>\w+)dfa","asbsadfasdfa").group())
  94. # 1 "1-2*(60+(-40.35/5)-(-4*3))"
  95. # 1.1 匹配所有的整数
  96. # s = "1-2*(60+(-40.35/5)-(-4*3))"
  97. # print(re.findall("\d+",s))
  98. # 匹配所有的数字(包含小数)
  99. # print(re.findall("\d+\.\d+|\d+",s))
  100. # 匹配所有的数字(包含小数包含负号)
  101. # print(re.findall("-?\d+\.\d+|-?\d+",s))
  102. # s = "http://blog.csdn.net/make164492212/article/details/51656638"
  103. # print(re.findall("h.*2/",s))
  104. s1 = '''
  105. 时间就是1995-04-27,2005-04-27
  106. 1999-04-27 老男孩教育创始人
  107. 老男孩老师 alex 1980-04-27:1980-04-27
  108. 2018-12-08
  109. '''
  110. # print(re.findall("\d+-\d+-\d+",s1))
  111. # 匹配qq号:腾讯从10000开始
  112. # qq = input("请输入QQ号:")
  113. # print(re.findall("[1-9][0-9]{4,9}",qq))
  114. s1 = '''
  115. <div id="cnblogs_post_body" class="blogpost-body"><h3><span style="font-family: 楷体;">python基础篇</span></h3>
  116. <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>
  117. <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>
  118. <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>
  119. <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>
  120. <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>
  121. <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>
  122. <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>
  123. <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>
  124. <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>
  125. <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>
  126. <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>
  127. <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>
  128. <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>
  129. <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>
  130. <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>
  131. <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>
  132. </div>
  133. '''
  134. # print(re.findall("<span(.*?)>",s1))
  135. # print(re.findall('<a href="(.*?)"',s1))

2.今日总结

  1. # re -- 正则表达式
  2. import re
  3. # re.findall()
  4. # re.search()
  5. # re.match()
  6. # re.sub()
  7. # re.split()
  8. # re.finditer()
  9. # \w
  10. # \d
  11. # ^
  12. # $
  13. # m()t m(?:)t
  14. # [] [^]
  15. # {}
  16. # | 或者
  17. # \. == 转义成普通的.
  18. # * 0或多
  19. # + 1或多
  20. # ? 0或1
  21. # *? 限制*
  22. # .* 一个任意元素重复出现0次或多次
  23. # 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. (五)TestNG测试的并发执行详解

    原文链接:https://blog.csdn.net/taiyangdao/article/details/52159065 TestNG在执行测试时,默认suitethreadpoolsize=1, ...

  2. mysql小数类型

    原文链接:https://blog.csdn.net/weixin_42047611/article/details/81449663 MySQL 中使用浮点数和定点数来表示小数. 浮点类型有两种,分 ...

  3. Java 源码刨析 - HashMap 底层实现原理是什么?JDK8 做了哪些优化?

    [基本结构] 在 JDK 1.7 中 HashMap 是以数组加链表的形式组成的: JDK 1.8 之后新增了红黑树的组成结构,当链表大于 8 并且容量大于 64 时,链表结构会转换成红黑树结构,它的 ...

  4. 全网最全fiddler使用教程和fiddler如何抓包(fiddler手机抓包)-笔者亲测

    一.前言 抓包工具有很多,比如常用的抓包工具Httpwatch,通用的强大的抓包工具Wireshark.为什么使用fiddler?原因如下:1.Wireshark是通用的抓包工具,但是比较庞大,对于只 ...

  5. git 的使用:

    git  的官方网址是: https://git-scm.com/downloads github 的官方网址: https://github.com/ git 的原理图: git 和 github ...

  6. 解决github打不开问题

    2020.06.22 使用以下方式: 在https://github.com.ipaddress.com/找到: 在https://fastly.net.ipaddress.com/github.gl ...

  7. ECSHOP后台左侧添加菜单栏

    比如我们在后台中增加 “活动管理”功能,方法如下 在ECSHOP 管理中心共用语言文件 language\zh_cn\admin\commn.php ,添加我们的自定义菜单: $_LANG['17_a ...

  8. Python-argparse模块-获取命令行参数

    #!/usr/bin/python3 """ Author : Jet Bi License : www.cyeap.com Summary : 获取命令行的参数 Not ...

  9. 【Vim命令】

    命令 操作 :set nu 显示行号  i 编辑模式  :wq  修改并退出  :%s/a/b  把所有的a替换成b                        

  10. ceph rbd块存储挂载及文件存储建立

    一.rbd块存储挂载 1 创建一个OSD pool # ceph osd pool create rbd1 128 查询存储空间使用 # ceph df GLOBAL: SIZE AVAIL RAW ...