正则表示式对象

对象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,

有幸看到我博客的朋友们,若能学到知识,请多多关注以及讨论,让我们共同进步,扬帆起航。

后记:打油诗一首

适度锻炼,量化指标

考量天气,设定目标

科学锻炼,成就体标

高效科研,实现学标


 
 
 
 

最新文章

  1. Java学习——连接数据库
  2. SQL Server自动化运维系列——批量执行SQL脚本(Power Shell)
  3. UWP&amp;WP8.1 重新绘制图片 WriteableBitmap用法 图片转byte[]数组,byte[]数组转图片
  4. shell脚本调试
  5. Java中的链表数据结构
  6. 《TCP/IP详解 卷一》读书笔记-----TCP数据流
  7. 用ie调试的时候显示:脚本调试程序无法连接到目标进程,已附加调试程序。
  8. Mysql大数据量查询优化
  9. 云服务器 ECS Linux 系统添加“回收站”
  10. How to Run Node.js with Express on Mobile Devices
  11. 初学c++
  12. CSS Hack (各个浏览器兼容的问题)
  13. OOAD基本概念
  14. java udp socket通信(仅发送)
  15. Vue.js 基础指令实例讲解(各种数据绑定、表单渲染大总结)——新手入门、高手进阶
  16. PHP高并发
  17. Analysis Services(SSAS) 性能优化
  18. 让wampserver2.5.exe支持sql server数据库的方法
  19. mod_fcgid: HTTP request length 136136 (so far) exceeds MaxRequestLen (131072)
  20. 周鸿祎与85后的座谈(一):人人需要Mentor,世界没有奇迹

热门文章

  1. [转]Linux下的链接脚本基础
  2. Linux动态频率调节系统CPUFreq之一:概述【转】-- 非常好的博客
  3. opencv学习笔记(九)Mat 访问图像像素的值
  4. Numpy详解
  5. Coursera台大机器学习技法课程笔记07-Blending and Bagging
  6. 【ES】match_phrase与regexp
  7. 2018-2019-2 网络对抗week1 Kali安装 20165333陈国超
  8. LICEcap方便快捷制作gif图片的工具
  9. HTML5游戏 围住神经猫 开发
  10. Android开发之实现多次点击事件