详解python里使用正则表达式的全匹配功能

作者:caimouse 时间:2023-12-29 17:10:14 

详解python里使用正则表达式的全匹配功能

python中很多匹配,比如搜索任意位置的search()函数,搜索边界的match()函数,现在还需要学习一个全匹配函数,就是搜索的字符与内容全部匹配,它就是fullmatch()函数。

例子如下:


#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
import re

text = 'This is some text -- with punctuation.'
pattern = 'is'

print('Text    :', text)
print('Pattern  :', pattern)

m = re.search(pattern, text)
print('Search   :', m)
s = re.fullmatch(pattern, text)
print('Full match :', s)

text = 'is'
print('Text    :', text)
s = re.fullmatch(pattern, text)
print('Full match :', s)

text = 'iss'
print('Text    :', text)
s = re.fullmatch(pattern, text)
print('Full match :', s)

结果输出如下:


Text    : This is some text -- with punctuation.
Pattern  : is
Search   : <_sre.SRE_Match object; span=(2, 4), match='is'>
Full match : None
Text    : is
Full match : <_sre.SRE_Match object; span=(0, 2), match='is'>
Text    : iss
Full match : None

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:http://blog.csdn.net/caimouse/article/details/78252504

标签:python,正则表达式,匹配
0
投稿

猜你喜欢

  • JavaScript选取(picking)和反选(rejecting)对象的属性方法

    2023-08-24 22:28:47
  • 最新IntelliJ IDEA 2020.2永久激活码(亲测有效)

    2023-07-09 01:45:14
  • Jupyter Notebook读入csv文件时出错的解决方案

    2021-09-15 18:13:48
  • Python基础之python循环控制语句break/continue详解

    2022-01-24 05:54:01
  • 在Mac OS系统上安装Python的Pillow库的教程

    2021-09-29 15:03:26
  • 交互设计杂七杂八

    2010-09-25 18:41:00
  • 深入浅析PHP的session反序列化漏洞问题

    2024-05-13 09:51:14
  • 详解Go语言中的数据库操作

    2024-01-15 19:30:23
  • python中的psutil模块详解(cpu、内存、磁盘情况、结束指定进程)

    2021-12-19 23:33:04
  • 解决pandas .to_excel不覆盖已有sheet的问题

    2022-12-02 22:42:25
  • Python中多线程的创建及基本调用方法

    2021-12-14 00:26:26
  • Go语言学习之数组的用法详解

    2024-04-26 17:18:34
  • MSSQL安全设置的具体步骤和方法小结

    2012-07-11 15:54:11
  • 3个JS控制图片滚动的效果

    2007-10-23 13:40:00
  • Python协程的2种实现方式分享

    2022-12-21 12:42:56
  • JavaScript常见数组方法之如何转置矩阵

    2024-04-17 10:37:41
  • Python基于动态规划算法解决01背包问题实例

    2021-01-10 21:22:26
  • IE中jscript/javascript的条件编译

    2007-10-03 14:03:00
  • JavaScript中new操作符的原理与实现详解

    2024-05-22 10:31:07
  • pygame 键盘事件的实践

    2023-09-29 18:56:10
  • asp之家 网络编程 m.aspxhome.com