Python3正则表示式(3)
2018-06-28 05:38:52来源:博客园 阅读 ()
正则表示式对象
对象1:
案例1:
import re example = 'ShanDong Institute of Business and Technology' pattern = re.compile(r'\bB\w+\b') # 查找以B开头的单词 pattern.findall(example) # 结果:['Business'] pattern = re.compile(r'\w+g\b') # 查找以字母g结尾的单词 pattern.findall(example) # 结果:['ShanDong'] pattern = re.compile(r'\b[a-zA-Z]{3}\b') # 查找3个字母长的单词 pattern.findall(example) # 结果:['and'] pattern.search(example) # 结果:<_sre.SRE_Match object; span=(31, 34), match='and'> pattern = re.compile(r'\b\w*a\w*\b') # 查找所有含字母a的单词 pattern.findall(example) # 结果:['ShanDong', 'and'] text = 'He was carefully disguised but captured quickly by police.' re.findall(r'\w+ly', text) # 查找所有以字母组合ly结尾的单词 # 结果:['carefully', 'quickly']
对象2
案例2:
example = """Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts.""" pattern = re.compile(r'\bb\w*\b', re.I) print(pattern.sub('*', example)) # 结果 ''' * is * than ugly. Explicit is * than implicit. Simple is * than complex. Complex is * than complicated. Flat is * than nested. Sparse is * than dense. Readability counts. ''' print(pattern.sub(lambda x: x.group(0).upper(), example)) # 结果 ''' BEAUTIFUL is BETTER than ugly. Explicit is BETTER than implicit. Simple is BETTER than complex. Complex is BETTER than complicated. Flat is BETTER than nested. Sparse is BETTER than dense. Readability counts. ''' print(pattern.sub('#', example, 1)) # 结果 ''' # is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. '''
对象3
知识在于点点滴滴的积累,我会在这个路上Go ahead,
后记:打油诗一首
适度锻炼,量化指标
考量天气,设定目标
科学锻炼,成就体标
高效科研,实现学标
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- python学习-53 正则表达式 2019-08-13
- python3 enum模块的应用 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash