Python中正则表达式的用法总结

 更新时间:2019年02月22日 11:59:30   作者:topleeyap  
今天小编就为大家分享一篇关于Python中正则表达式的用法总结,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

正则表达式很神奇啊

# -*- coding:utf-8 -*-
import re
def print_match_res(res):
  """打印匹配对象内容"""
  if res is not None:
    print(res.group())
  else:
    print(None)
# 两种匹配方式:
pattern="[A-Z][a-z]+"
# 一、使用re模块函数进行匹配
res=re.match(pattern,"Tom is a good boy")     # 匹配,返回匹配对象
print(type(res))
print(res.group())
# 二、使用预编译后的正则表达式对象的方法进行匹配
obj_pattern=re.compile(pattern)   # 预编译,返回正则表达式对象
print(type(obj_pattern))
res=obj_pattern.match("Tom is a good boy")    # 匹配,返回匹配对象
print(type(res))
print(res.group())
# 匹配对象的group()和groups()方法
pattern="\d{3}-\d{5}"
obj_pattern=re.compile(pattern)
res=obj_pattern.search("家庭电话:000-88886")
print(res.group())   # 返回整个匹配或特定子组
print(res.groups())   # 返回包含全部子组的元组
# match():从起始部分开始匹配,如果成功,返回匹配对象;失败,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).match("my name is li")
res=re.match(pattern,"my name is li")
print_match_res(res)
# search(): 从任意位置开始匹配,如果成功,返回匹配对象;失败,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).search("it's my dog")
res=re.search(pattern,"my name is li")
print_match_res(res)
# 查找全部
# findall(),finditer()
res=re.findall(r"th\w+","This and that",re.I)
print(res)
res=re.finditer(r"th\w+","This and that",re.I)
print(res)
print(next(res).group(),next(res).group())
# 替换
# sub(),subn()
res=re.sub("funny","fool","You are so funny")
print(res)
res=re.subn("funny","fool","You are so funny")
print(res)
# 分割
# splite()
res=re.split("\.","Mr.Smith")
print(res)
print("#"*50)
# 择一匹配符号 a|b
pattern="I|You|She"
res=re.compile(pattern,flags=re.IGNORECASE).match("i love you")
print_match_res(res)
res=re.compile(pattern,flags=re.I).search("who love you")
print_match_res(res)
# 匹配任意单个字符 .
pattern="w{3,}\..+\.com"
res=re.match(pattern,"wwww.google.com/index.html",re.I)
print_match_res(res)
# 字符集 [abc] [a-z0-9]
pattern="[A-Za-z0-9_]*\."
res=re.match(pattern,"Python3.?")
print_match_res(res)
# 特殊字符 \d \w \s \b \\
# 重复 + ? * {N,} {N,M}
# 分组 (...)
pattern="\w+@(\w{1,10}\.)*([a-z]*)"
res=re.match(pattern,"li@gmail.com")
print_match_res(res)
res=re.match(pattern,"li@qq.vip.org")
print_match_res(res)
print(res.group(0),res.group(1),res.group(2),sep="\t")
print(res.groups())
# 匹配字符串的起始和结尾,单词边界 ^a z$ \A \Z \b \B
pattern=r"^the"
# pattern=r"\Athe"
res=re.search(pattern,"The end of the world")
print_match_res(res)
res=re.search(pattern,"they smile")
print_match_res(res)
pattern=r"cry$"
# pattern=r"cry\Z"
res=re.search(pattern,"they cry")
print_match_res(res)
res=re.search(r"\bthe","bit the dog")
print_match_res(res)
res=re.search(r"\Bhe","bit the dog")
print_match_res(res)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • pytorch搭建模型的五大层次级别解读(由浅入深)

    pytorch搭建模型的五大层次级别解读(由浅入深)

    这篇文章主要介绍了pytorch搭建模型的五大层次级别(由浅入深),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • pytorch实现用CNN和LSTM对文本进行分类方式

    pytorch实现用CNN和LSTM对文本进行分类方式

    今天小编就为大家分享一篇pytorch实现用CNN和LSTM对文本进行分类方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Django media static外部访问Django中的图片设置教程

    Django media static外部访问Django中的图片设置教程

    这篇文章主要介绍了Django media static外部访问Django中的图片设置教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Pandas数据连接pd.concat的实现

    Pandas数据连接pd.concat的实现

    本文主要介绍了Pandas数据连接pd.concat的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • python使用matplotlib定制绘图的线型、标记类型

    python使用matplotlib定制绘图的线型、标记类型

    这篇文章主要给大家详细介绍了python使用matplotlib定制绘图的线型、标记类型,文中有详细的代码示例,具有一定的参考价值,需要的朋友可以参考下
    2023-07-07
  • 关于numpy和torch.tensor的张量的操作

    关于numpy和torch.tensor的张量的操作

    这篇文章主要介绍了关于numpy和torch.tensor的张量的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • python使用knn实现特征向量分类

    python使用knn实现特征向量分类

    这篇文章主要为大家详细介绍了python使用knn实现特征向量分类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Python实现简繁体转换

    Python实现简繁体转换

    很多时候简繁体转换,掌握了简体与繁体的转换,往往能够事半功倍,本文主要介绍了Python实现简繁体转换,感兴趣的可以了解一下
    2021-06-06
  • Python零钱兑换的实现代码

    Python零钱兑换的实现代码

    假如有这样一个问题给你一个整数数组 coins ,表示不同面额的硬币以及一个整数 amount ,表示总金额,计算并返回可以凑成总金额所需的最少的硬币个数,接下来通过示例代码给大家介绍Python零钱兑换问题,感兴趣的朋友一起看看吧
    2022-05-05
  • Python编程入门的一些基本知识

    Python编程入门的一些基本知识

    这篇文章主要介绍了Python编程入门的一些基本知识,包括注释需和Shell命令使用等基本内容,要的朋友可以参考下
    2015-05-05

最新评论