正则表达式在python的中的应用(Python中正则表达式的用法总结)
类别:脚本大全 浏览量:2856
时间:2022-01-24 00:29:35 正则表达式在python的中的应用
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) |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/topleeyap/article/details/78846099
您可能感兴趣
- 怎么python爬取网页图片(详解Python静态网页爬取获取高清壁纸)
- python实现层次遍历二叉树(Python实现的序列化和反序列化二叉树算法示例)
- wxpython控件如何移动(基于wxPython的GUI实现输入对话框1)
- pythonpickle使用方法(Python supervisor强大的进程管理工具的使用)
- java集成钉钉发送消息(Python实现钉钉发送报警消息的方法)
- python 模式识别(python实现全盘扫描搜索功能的方法)
- python 写入d盘文件(python文件写入write的操作)
- 基于pythonopencv的图片识别(Python Opencv实现图像轮廓识别功能)
- pythondjango搭建web(PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上)
- pythonzipfile的用法(对Python之gzip文件读写的方法详解)
- python 取出时间段日志(python 实现提取某个索引中某个时间段的数据方法)
- python创建hbase命名空间(python使用phoenixdb操作hbase的方法示例)
- python数据类型基本知识入门(Python Pexpect库的简单使用方法)
- python单例编程(浅谈Python反射 & 单例模式)
- pythonmatplotlib条形图动画(Python Matplotlib实现三维数据的散点图绘制)
- python怎么去掉字符串的空格(Python切片操作去除字符串首尾的空格)
- 郭麒麟(郭麒麟)
- 古人十句 戒骄 名言,醍醐灌顶,受益匪浅(古人十句戒骄名言)
- 《道德经》:功成不局,泰而不骄(道德经:功成不局)
- 每日一典 过江之鲫(每日一典过江之鲫)
- 红色代表什么(红色代表什么意义和象征)
- 菲律宾安全吗(菲律宾安全吗2023)
热门推荐
- windows搭建php环境(windows 2008r2+php5.6.28环境搭建详细过程)
- php联合注入列表总结(php中的依赖注入实例详解)
- mysql的字符串截取函数(MySQL实现字符串的拼接,截取,替换,查找位置的操作)
- python如何遍历列表并提取(Python同步遍历多个列表的示例)
- html5清除浮动的方法(HTML5实现移动端点击翻牌功能)
- , , 的区别
- css继承属性有哪些(CSS特殊性、继承与层叠)
- 了解mysql数据自动备份(MySQL数据备份方法的选择与思考)
- 网络虚拟机系统安装教程(NPOINT免费虚拟主机管理系统windows2003的安装方法)
- apache配置详解(apache2.2和php5.2.17在windows下整合过程的错误解决方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9