Python3正则表示式(3)
正则表示式对象
对象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)的更多相关文章
- Python3漏洞扫描工具 ( Python3 插件式框架 )
目录 Python3 漏洞检测工具 -- lance screenshot requirements 关键代码 usage documents README Guide Change Log TODO ...
- 基于Python3的漏洞检测工具 ( Python3 插件式框架 )
目录 Python3 漏洞检测工具 -- lance screenshot requirements 关键代码 usage documents Any advice or sugggestions P ...
- js中split 正则表示式 (/[,+]/)
定义和用法 split() 方法用于把一个字符串分割成字符串数组. 语法 stringObject.split(separator,howmany) separator 作为分隔符,separator ...
- Python学习之路 (五)爬虫(四)正则表示式爬去名言网
爬虫的四个主要步骤 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的方式存储和使用) 什么是正则表达式 ...
- [19/05/04-星期六] 正则表示式(Regular Expression)
一.概念 语法: \D :就是不是0-9数字的其它字符: \W:与\w相反: a\d?b:表示在字符a和b之间可以有一个数字或者没有数字都可以:如:ab .a3b a\d+b:表示在字符a和b之间至少 ...
- python3+ 模块学习 之 re
re 模块 参考:Python3 如何优雅地使用正则表达式(详解系列) Python3 正则表达式特殊符号及用法(详细列表) (出处: 鱼C论坛) 正则表达式 常用元字符:. ^ $ * + ? ...
- python3 爬虫笔记(一)beautiful_soup
很多人学习python,爬虫入门,在python爬虫中,有很多库供开发使用. 用于请求的urllib(python3)和request基本库,xpath,beautiful soup,pyquery这 ...
- Windows下安装Python3和Django
下载python3 首先去 python的官网 下载最新稳定版的python3, 我下载的时候python3的最新版本是3.6.5. 亦可点击 此链接 直接下载. 安装python3 傻瓜式安装,注意 ...
- Python3正则表达式(4)
正则表示式的子模式 使用()表示一个子模式,括号中的内容作为一个整体出现. (red)+ ==> redred, redredred, 等多个red重复的情况 子模式的扩展语法 案例1 tel ...
随机推荐
- Statistics in Python
Statistics in Python Materials for the “Statistics in Python” euroscipy 2015 tutorial. Requirements ...
- C++ Boost库简介(转载)
boost是一个准标准库,相当于STL的延续和扩充,它的设计理念和STL比较接近,都是利用泛型让复用达到最大化.不过对比STL,boost更加实用.STL集中在算法部分,而boost包含了不少工具类, ...
- 异步编程之使用yield from
异步编程之使用yield from yield from 是 Python3.3 后新加的语言结构.yield from的主要功能是打开双向通道,把最外层的调用方法与最内层的子生成器连接起来.这两者就 ...
- jvm系列四、jvm知识点总结
原文链接:http://www.cnblogs.com/ityouknow/p/6482464.html jvm 总体梳理 jvm体系总体分四大块: 类的加载机制 jvm内存结构 GC算法 垃圾回收 ...
- uoj#188. 【UR #13】Sanrd(Min_25筛)
题面 传送门 题解 这是一道语文题 不难看出,题目所求即为\(l\)到\(r\)中每个数的次大质因子 我们考虑\(Min\_25\)筛的过程,设 \[S(n,j)=\sum_{i=1}^nsec_p( ...
- 使用ado.net打造通用的数据库操作类
最近在项目中使用中碰到了这样一种情况,查询的数据是从Oracle中获取的,但是记录下来的数据是存在Sql Server中(企业Oracle数据库管理太严,没办法操作).而且我在之前的工作中也碰到过使用 ...
- web服务器tomcat入门实战
一.tomcat介绍1.1 引入tomcat Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Su ...
- 随机森林学习-2-sklearn
# -*- coding: utf-8 -*- """ RandomForestClassifier skleran的9个模型在3份数据上的使用. 1. 知识点: skl ...
- vue-router之路由钩子(组件内路由钩子必须在路由组件调用,子组件没用)
模式 vue-router中的模式选项主要在router实例化的时候进行定义的,如下 const router = new VueRouter({ mode: 'history', // 两种类型hi ...
- 目标检测-yolo
论文下载:http://arxiv.org/abs/1506.02640 代码下载:https://github.com/pjreddie/darknet 1.创新点 端到端训练及推断 + 改革区域建 ...