Python中正则表达式的用法总结
Python  /  管理员 发布于 7年前   180
正则表达式很神奇啊
# -*- coding:utf-8 -*-import redef 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|bpattern="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,"[email protected]")print_match_res(res)res=re.match(pattern,"[email protected]")print_match_res(res)print(res.group(0),res.group(1),res.group(2),sep="\t")print(res.groups())# 匹配字符串的起始和结尾,单词边界 ^a z$ \A \Z \b \Bpattern=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)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家的支持。如果你想了解更多相关内容请查看下面相关链接
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号