正则表示式对象

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

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

后记:打油诗一首

适度锻炼,量化指标

考量天气,设定目标

科学锻炼,成就体标

高效科研,实现学标


 
 
 
 

Python3正则表示式(3)的更多相关文章

  1. Python3漏洞扫描工具 ( Python3 插件式框架 )

    目录 Python3 漏洞检测工具 -- lance screenshot requirements 关键代码 usage documents README Guide Change Log TODO ...

  2. 基于Python3的漏洞检测工具 ( Python3 插件式框架 )

    目录 Python3 漏洞检测工具 -- lance screenshot requirements 关键代码 usage documents Any advice or sugggestions P ...

  3. js中split 正则表示式 (/[,+]/)

    定义和用法 split() 方法用于把一个字符串分割成字符串数组. 语法 stringObject.split(separator,howmany) separator 作为分隔符,separator ...

  4. Python学习之路 (五)爬虫(四)正则表示式爬去名言网

    爬虫的四个主要步骤 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的方式存储和使用) 什么是正则表达式 ...

  5. [19/05/04-星期六] 正则表示式(Regular Expression)

    一.概念 语法: \D :就是不是0-9数字的其它字符: \W:与\w相反: a\d?b:表示在字符a和b之间可以有一个数字或者没有数字都可以:如:ab .a3b a\d+b:表示在字符a和b之间至少 ...

  6. python3+ 模块学习 之 re

    re 模块 参考:Python3 如何优雅地使用正则表达式(详解系列) Python3 正则表达式特殊符号及用法(详细列表)    (出处: 鱼C论坛) 正则表达式 常用元字符:. ^ $ * + ? ...

  7. python3 爬虫笔记(一)beautiful_soup

    很多人学习python,爬虫入门,在python爬虫中,有很多库供开发使用. 用于请求的urllib(python3)和request基本库,xpath,beautiful soup,pyquery这 ...

  8. Windows下安装Python3和Django

    下载python3 首先去 python的官网 下载最新稳定版的python3, 我下载的时候python3的最新版本是3.6.5. 亦可点击 此链接 直接下载. 安装python3 傻瓜式安装,注意 ...

  9. Python3正则表达式(4)

    正则表示式的子模式 使用()表示一个子模式,括号中的内容作为一个整体出现. (red)+  ==> redred, redredred, 等多个red重复的情况 子模式的扩展语法 案例1 tel ...

随机推荐

  1. 【python图像处理】图像的缩放、旋转与翻转

    [python图像处理]图像的缩放.旋转与翻转 图像的几何变换,如缩放.旋转和翻转等,在图像处理中扮演着重要的角色,python中的Image类分别提供了这些操作的接口函数,下面进行逐一介绍. 1.图 ...

  2. C++:greater<int>和less<int>

    greater和less是xfunctional.h中的两个结构体,代码如下: template<class _Ty = void> struct less { // functor fo ...

  3. Android开源动画库nineoldandroids

    项目官网地址:http://nineoldandroids.com/ 使用这个库的原因是android3.0之后出了新的animation API,但是android3.0以下的不支持 这个库完成了这 ...

  4. JavaScript的类型自动转换高级玩法JSFuck

    0 前言 最开始是不小心在微信公众号(程序员大咖)看到一篇JS的高逼格代码,然后通过里面的链接跳转到了JSFuck的wiki,就像顺着迷宫找宝藏的感觉,感叹JS的自动类型转换的牛逼. 1 样例 (!( ...

  5. 改变input标签中placeholder显示的颜色

    ::-webkit-input-placeholder { /* WebKit browsers */ color: #A9A9A9; } :-moz-placeholder { /* Mozilla ...

  6. python 全栈开发,Day73(django多表添加,基于对象的跨表查询)

    昨日内容回顾 多表方案: 如何确定表关系呢? 表关系是在2张表之间建立的,没有超过2个表的情况. 那么相互之间有2条关系线,先来判断一对多的关系. 如果其中一张表的记录能够对应另外一张表的多条记录,那 ...

  7. 遍历集合的Iterator删除其中的元素

    package list; import java.util.LinkedList; /* * 遍历集合的时候删除其中的元素 从后往前删,每次都删除的是最后一个元素,不涉及移位 */public cl ...

  8. 安装httpd过程,将网站部署到httpd过程

    1,配置DNSvi /etc/resolv.conf 加入以下代码 nameserver 192.168.0.1 nameserver 8.8.8.8 nameserver 8.8.4.4 2.输入y ...

  9. Oracle学习笔记--第3章 使用sql*plus工具

    使用sql*plus工具 1.sqlplus连接方式示例 sqlplus system/123[as sysdba]; 2.查看表结构命令;desc[ribe] e.g.      desc scot ...

  10. DDD模型领域WF到领域层(十五)

    实现超市的结算系统: 计算相应的优惠方式的接口 public interface ICompute { double GetResultTotalMoney(double TotalMoney); } ...