常见python正则用法的简单实例
作者:jingxian 发布时间:2022-09-17 07:21:36
下面列出Python正则表达式的几种匹配用法:
1.测试正则表达式是否匹配字符串的全部或部分
regex=ur"" #正则表达式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()
2.测试正则表达式是否匹配整个字符串
regex=ur"\Z" #正则表达式末尾以\Z结束
if re.match(regex, subject):
do_something()
else:
do_anotherthing()
3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group()
else:
result = ""
5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group(1)
else:
result = ""
6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""
7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
8.遍历所有匹配的子串(Iterate over all matches in a string)
for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)
reobj = re.compile(regex)
10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)
reobj = re.compile(regex)
if reobj.search(subject):
do_something()
else:
do_anotherthing()
11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)
reobj = re.compile(r"\Z")#正则表达式末尾以\Z 结束
if reobj.match(subject):
do_something()
else:
do_anotherthing()
12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group()
else:
result = ""
14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group(1)
else:
result = ""
15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group("groupname")
else:
result = ""
16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)
reobj = re.compile(regex)
result = reobj.findall(subject)
17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)
reobj = re.compile(regex)
for match in reobj.finditer(subject):
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
字符串替换
1.替换所有匹配的子串
#用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject)
2.替换所有匹配的子串(使用正则表达式对象)
reobj = re.compile(regex)
result = reobj.sub(newstring, subject)
字符串拆分
1.字符串拆分
result = re.split(regex, subject)
2.字符串拆分(使用正则表示式对象)
reobj = re.compile(regex)
result = reobj.split(subject)


猜你喜欢
- 接着上一篇,这里继续整合交易类。import datetime#交易类,后期需要整合公钥,私钥class Transaction: &nbs
- 一、数据创建1.tf.constant()创建自定义类型,自定义形状的数据,但不能创建类似于下面In [59]这样的,无法解释的数据。2.t
- 按照本文操作和体会,会对sql优化有个基本最简单的了解,其他深入还需要更多资料和实践的学习: 1. 建表: 代码如下:creat
- 样式使用的是vux的cell组件 如下图的官方demo样子上图的样式需要修改一下,把 保护中 修改成一个图片 并且内嵌一个input typ
- 附上代码与运行结果截图:import time# 获取当前时间now = time.localtime()# 格式化日期now_ = tim
- 本文实例为大家分享了雪花飞舞效果javascript实现,供大家参考,具体内容如下原理:1、js动态创建DIV,指定CLASS类设置不同的背
- 本文带你快速了解@Async注解的用法,包括异步方法无返回值、有返回值,最后总结了@Async注解失效的几个坑。在 SpringBoot 应
- 本篇博客主要介绍的是pyinstaller在windows下的基本使用和基础避坑在windows中使用pyinstaller工具打包时会出现
- 首先还是应该科普下函数参数传递机制,传值和传引用是什么意思?函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进
- 最近做网站期间遇到个问题,就是用到比较流行的lightbox效果,就是点击链接后会弹出个固定大小窗口,弹出后窗口下面的内容是被遮罩掉的,最近
- 一、安装Bautiful Soup 是第三方库,因此需要单独下载,下载方式非常简单由于 BS4 解析页面时需要依赖文档解析器,所以还需要安装
- 前言相信看到这个题目,可能大家都觉得是一个老生常谈的月经topic了。一直以来其实把握一个“值传递”基本上就能理解各种情况了,不过最近遇到了
- 首先,需要获取任意知乎的问题,只需要你输入问题的ID,就可以获取相关的页面信息,比如最重要的合计有多少人回答问题。问题ID为如下标红数字编写
- 一、怎么样取得最新版本的MySQL?要安装MySQL,首先要当然要取得它的最新版本,虽然大家都知道在FreeBSD的Packages中可以找
- 目录vuex持久化总结vuex持久化vuex:刷新浏览器,vuex中的state会重新变为初始状态解决办法:使用vuex-persisted
- 本文实例讲述了Yii2 assets清除缓存的方法。分享给大家供大家参考,具体如下:use vendor\myVendorName\myPa
- 异常捕捉:try: XXXXX1 raise Exception(“xxxxx2”) except (Except
- 前言今天带大家爬取王者荣耀全套皮肤,废话不多说,直接开始~开发工具Python版本: 3.6.4相关模块:requests模块;urllib
- 大家好,今天才发现很多学习Flask的小伙伴都有这么一个问题,清理缓存好麻烦啊,今天就教大家怎么解决。大家在使用Flask静态文件的时候,每
- Python 中的 Operator 模块可以让它支持函数式编程。1 计算函数假设我们需要一个计算阶乘的函数,一般做法是使用递归。如果使用函