30-Python3 正则表达式

  1. '''
  2. re.match函数
  3. '''
  4. import re
  5. print(re.match('www','www.runoob.com').span())
  6. print(re.match('ww','www.runoob.com').span())
  7. print(re.match('w','www.runoob.com').span())
  8. print(re.match('com','www.runoob.com'))
  9.  
  10. line = 'Cats are smarter than dogs'
  11. macthObj = re.match(r'(.*)are(.*?).*',line,re.M|re.I)
  12. if macthObj:
  13. print('matchObj.group():',macthObj.group())
  14. print('matchObj.group(1):',macthObj.group(1))
  15. print('matchObj.group(2):',macthObj.group(2))
  16. else:
  17. print('No match')
  18.  
  19. '''
  20. re.search方法
  21. '''
  22. import re
  23. print(re.search('www','www.runoob.com').span())
  24. print(re.search('com','www.runoob.com').span())
  25.  
  26. line1 = 'Cats are smarter than dogs'
  27. searchObj = re.search(r'(.*)are(.*?).*',line1,re.M|re.I)
  28. if searchObj:
  29. print('searchObj.group():',searchObj.group())
  30. print('searchObj.group(1):',searchObj.group(1))
  31. print('searchObj.group(2):',searchObj.group(2))
  32. else:
  33. print('Nothing found!')
  34.  
  35. '''
  36. re.match和re.search的区别
  37. '''
  38. line2 = 'Cats are smarter than dogs'
  39. matchObj = re.match(r'dogs',line2,re.M|re.I)
  40. if matchObj:
  41. print('re.match:',matchObj.group())
  42. else:
  43. print('no match1')
  44. matchObj = re.search(r'dogs',line2,re.M|re.I)
  45. if matchObj:
  46. print('re.search:',matchObj.group())
  47. else:
  48. print('no match2')
  49. '''
  50. 检索和替换
  51. '''
  52. phone = '2004-959-559 #这是一个电话号码'
  53. ##删除注释
  54. num = re.sub(r'#.*$','',phone)
  55. print('电话号码1:',num)
  56. ##移除非数字的内容
  57. num = re.sub(r'\D','',phone)
  58. print('电话号码2:',num)
  59.  
  60. '''
  61. repl参数是一个函数
  62. '''
  63.  
  64. #将匹配到到数字乘以2
  65. def double(matched):
  66. value = int(matched.group('value'))
  67. return str(value*2)
  68.  
  69. s = 'QAA342RFDFD56FGFG'
  70. print(re.sub('(?P<value>\d+)',double,s))
  71.  
  72. '''
  73. compile函数
  74. '''
  75. pattern1 = re.compile(r'\d+')
  76. m = pattern1.match('one12twothree34four')
  77. m1 = pattern1.search('one12twothree34four')
  78. print('m',m)
  79. print('m1',m1)
  80.  
  81. m2 =pattern1.match('one12twothree34four',2,10)
  82. print('m2',m2)
  83.  
  84. m3 =pattern1.match('one12twothree34four',3,10)
  85. print('m3:',m3)
  86.  
  87. print('m3.group():',m3.group())
  88. print('m3.start():',m3.start())
  89. print('m3.end():',m3.end())
  90. print('m3.span():',m3.span())
  91.  
  92. pattern2 = re.compile(r'([a-z]+)([a-z])',re.I) #re.I 表示忽略大小写
  93. mm = pattern2.match('Hello World Wide Web')
  94. print('mm:',mm)
  95.  
  96. print('mm.group(0):',mm.group(0))
  97. print('mm.span(0):',mm.span(0))
  98. print('mm.group(1):',mm.group(1))
  99. print('mm.span(1):',mm.span(1))
  100. print('mm.group(2):',mm.group(2))
  101. print('mm.span(2):',mm.span(2))
  102. print('mm.groups():',mm.groups())
  103. # print('mm.group(3):',mm.group(3))
  104.  
  105. '''
  106. findall
  107. '''
  108. # 在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
  109. # 注意: match 和 search 是匹配一次 findall 匹配所有。
  110. # 语法格式为:
  111. # findall(string[, pos[, endpos]])
  112. pattern3 = re.compile(r'\d+')
  113. result1 = pattern3.findall('runoob 123 google 456')
  114. result2 = pattern3.findall('run88oob123google456',0,10)
  115. print('result1:',result1)
  116. print('result2:',result2)
  117.  
  118. '''
  119. re.finditer:找到正则表达式所匹配的所有子串,并把他们作为一个迭代器返回
  120. '''
  121. it = re.finditer(r'\d+','qaz12edc34edc4rfv56')
  122. for match in it:
  123. print(match.group())
  124. '''
  125. re.split
  126. '''
  127. print('1:',re.split('\W+','runoob,runoob,runoob.'))
  128.  
  129. print('2:',re.split('(\W+)','runoob,runoob,runoob.'))
  130. print('',re.split('\W+','runoob,runoob,runoob.',1))
  131.  
  132. '''
  133. 正则表达式对象
  134. '''
  135.  
  136. '''
  137. 正则表达式修饰符-可选标志
  138. '''
  139.  
  140. '''
  141. 正则表达式模式
  142. '''
  143.  
  144. '''
  145. 正则表达式实例
  146. '''

30-Python3 正则表达式的更多相关文章

  1. 详解 Python3 正则表达式(五)

    上一篇:详解 Python3 正则表达式(四) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 非捕获组和命名 ...

  2. 详解 Python3 正则表达式(四)

    上一篇:详解 Python3 正则表达式(三) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 更多强大的功能 ...

  3. 详解 Python3 正则表达式(三)

    上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...

  4. 详解 Python3 正则表达式(二)

    上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...

  5. 详解 Python3 正则表达式(一)

    本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...

  6. python025 Python3 正则表达式

    Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...

  7. Python3 正则表达式

    字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...

  8. python3 正则表达式学习笔记

    re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. ~匹配成功re.match方法返回一个匹配的对象,否则返回No ...

  9. Python3正则表达式

    正则表达式是一个特殊的字符序列,他能帮助你方便的检查一个字符串是否与某种模式匹配.   re.match函数 re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,matc ...

  10. python3正则表达式总结

    转自csdn,去这里看更多吧: https://blog.csdn.net/weixin_40136018/article/details/81183504 还有一个废话很多的详细系列,在这里:htt ...

随机推荐

  1. SPL标准库-数据结构

    数据结构:栈 );] = ;] = ;var_dump($array); 来自为知笔记(Wiz)

  2. .NET Core开发日志——Runtime IDentifier

    .NET Core对于传统.NET开发人员而言是既熟悉又陌生的新平台,所以有时遇上出乎意料的事情也纯属正常情况.这时只需点耐心,多查查资料,努力找到原因,也未尝不是件有意义的体验. 比如当建完一个最简 ...

  3. 体验 ASP.NET Core 集成测试三剑客:xUnit.net、TestServer、EF Core InMemory

    这是昨天解决的一个问题,针对一个 web api 的客户端代理类写集成测试,既要测试 web api,又要测试 web api 客户端. 测试 web api,就要在运行测试时自动启动 web api ...

  4. 基于微服务架构、运行于容器中的.NET Core示例应用eShopOnContainers

    eShopOnContainers 是 <.NET Microservices – Architecture for Containerized .NET Applications>这本微 ...

  5. python-----双色球实现(实例1)

    #输出用户指定组数的双色球信息,其中一组信息 6个红色号码获取范围(1-33),1个蓝色号码获取范围(1-16),要求一组信息中红球内号码不可重复 import randomdef get_ball( ...

  6. c#加"\n\r"不换行,变成字符串

    质检模块,本想将每个错误分行, 比如:lyrerrormess += lyrname + "图层" + "缺少" + xmlFieldName + " ...

  7. 数据在内存中的存储方式( Big Endian和Little Endian的区别 )(x86系列则采用little endian方式存储数据)

    https://www.cnblogs.com/renyuan/archive/2013/05/26/3099766.html 1.故事的起源 “endian”这个词出自<格列佛游记>.小 ...

  8. [skill] vim 操作多个window

    前言: 分辨率越来越高,屏幕越来越大,行最长80不变,屏幕利用空白越来越大. 开多个window吧! 开window的命令: 平行开一个window:split <//path/file> ...

  9. 抽屉之Tornado实战(9)--装饰器实现用户登录状态验证

    当然今天讲的验证,不只Tornado会用,以后用到web框架都会用到,最常见的场景就是只有用户登陆了才能执行某些操作,所以在执行这些操作前要先做登陆状态的验证. 比如:点赞,发布,评论等需要验证,都需 ...

  10. 配置ASM以及创建恢复目录

    本次配置ASM沿用了搭建RAC的环境配置,系统选用CENTOS6.8 首先本地配置YUM,安装GRID集群件所需要的RPM包 [root@rac01 Packages]# cd /etc/yum.re ...