正则表达式在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中mat文件怎么读(Python第三方库h5py_读取mat文件并显示值的方法)
- pythonmatplotlib散点图怎么画(python使用matplotlib画柱状图、散点图)
- 用python简单实现登录功能(使用python的pexpect模块,实现远程免密登录的示例)
- python 百度搜索结果(Python模拟百度自动输入搜索功能的实例)
- python创建一个xls文件(Python XlsxWriter模块Chart类用法实例分析)
- python获取天气源(Python3爬虫之自动查询天气并实现语音播报)
- python制作彩色字符(Python3利用print输出带颜色的彩色字体示例代码)
- python中pip和pip3有什么区别(ISAPI-REWRITE伪静态规则写法以及说明)
- python多线程超时设置(解决python线程卡死的问题)
- python操作pandas(详解Python学习之安装pandas)
- 利用python合并pdf(Python合并同一个文件夹下所有PDF文件的方法)
- python实例之pyqt5多窗口实现(python+pyqt5编写md5生成器)
- python采集微信电脑端聊天信息(利用Python实现微信找房机器人实例教程)
- python中的多线程详解(python多线程抽象编程模型详解)
- 基于python的加密算法(python实现维吉尼亚加密法)
- ubuntu python3.7安装(详解Ubuntu16.04安装Python3.7及其pip3并切换为默认版本)
- 吉林舒兰 封城 聚集性疫情传播链已延至沈阳,有一个细节让人忧心(吉林舒兰封城)
- 1天密接者猛增77人,患者轨迹透露危险信号 吉林市全面封闭管理(1天密接者猛增77人)
- 吉林舒兰 封城 15人确诊 276人隔离,出现跨省传播(吉林舒兰封城)
- 四月新番CP人气榜公布,《剃须》两度上榜,沙优不是女朋友(四月新番CP人气榜公布)
- 2019年外媒秋季新番动画角色CP排行榜,桐人和爱丽丝落榜(2019年外媒秋季新番动画角色CP排行榜)
- 新一小兰领衔 盘点动漫中的那些 远距离恋爱情侣(盘点动漫中的那些)
热门推荐
- SQL中Truncate的用法
- 织梦dede怎么获取当前文档的网址(比较详细全面的织梦DEDECMS目录结构说明)
- uniapp 全局变量(如何在uniapp项目中使用mqtt)
- docker 搭建gitlab(使用Docker搭建私有GitLab的两种方案)
- php集成支付(ThinkPHP框架整合微信支付之刷卡模式图文详解)
- vue移动端返回在指定位置(vue移动端判断手指在屏幕滑动方向)
- SQL Server数据库备份的几种方式
- python 多进程读取文件(Python实现的多进程拷贝文件并显示百分比功能示例)
- mysql8.0.18.0安装详细教程(mysql 8.0.22 下载安装配置方法图文教程)
- css怎样设置文字不换行(css是如何实现在页面文字不换行、自动换行、强制换行的方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9