Python3正则匹配re.split,re.finditer及re.findall函数用法详解
作者:Citizen_Wang 发布时间:2023-06-27 10:13:48
本文实例讲述了Python3正则匹配re.split,re.finditer及re.findall函数用法。分享给大家供大家参考,具体如下:
re.split re.finditer re.findall
@(python3)
官方 re 模块说明文档
re.compile() 函数
编译正则表达式模式,返回一个对象。可以把常用的正则表达式编译成正则表达式对象,方便后续调用及提高效率。
re 模块最离不开的就是 re.compile 函数。其他函数都依赖于 compile 创建的 正则表达式对象
re.compile(pattern, flags=0)
pattern 指定编译时的表达式字符串
flags 编译标志位,用来修改正则表达式的匹配方式。支持 re.L|re.M 同时匹配
flags 标志位参数
re.I(re.IGNORECASE)
使匹配对大小写不敏感
re.L(re.LOCAL)
做本地化识别(locale-aware)匹配
re.M(re.MULTILINE)
多行匹配,影响 ^ 和 $
re.S(re.DOTALL)
使 . 匹配包括换行在内的所有字符
re.U(re.UNICODE)
根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.
re.X(re.VERBOSE)
该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。
示例:
import re
content = 'Citizen wang , always fall in love with neighbour,WANG'
rr = re.compile(r'wan\w', re.I) # 不区分大小写
print(type(rr))
a = rr.findall(content)
print(type(a))
print(a)
findall 返回的是一个 list 对象
<class '_sre.SRE_Pattern'>
<class 'list'>
['wang', 'WANG']
re.split 函数
按照指定的 pattern 格式,分割 string 字符串,返回一个分割后的列表。
re.split(pattern, string, maxsplit=0, flags=0)
pattern compile 生成的正则表达式对象,或者自定义也可
string 要匹配的字符串
maxsplit 指定最大分割次数,不指定将全部分割
import re
str = 'say hello world! hello python'
str_nm = 'one1two2three3four4'
pattern = re.compile(r'(?P<space>\s)') # 创建一个匹配空格的正则表达式对象
pattern_nm = re.compile(r'(?P<space>\d+)') # 创建一个匹配空格的正则表达式对象
match = re.split(pattern, str)
match_nm = re.split(pattern_nm, str_nm, maxsplit=1)
print(match)
print(match_nm)
结果:
['say', ' ', 'hello', ' ', 'world!', ' ', 'hello', ' ', 'python']
['one', '1', 'two2three3four4']
re.findall() 方法
返回一个包含所有匹配到的字符串的列表。
pattern 匹配模式,由 re.compile 获得
string 需要匹配的字符串
import re
str = 'say hello world! hello python'
pattern = re.compile(r'(?P<first>h\w)(?P<symbol>l+)(?P<last>o\s)') # 分组,0 组是整个 world!, 1组 or,2组 ld!
match = re.findall(pattern, str)
print(match)
结果
[('he', 'll', 'o '), ('he', 'll', 'o ')]
re.finditer 、re.findall
re.finditer(pattern, string[, flags=0])
re.findall(pattern, string[, flags=0])
pattern compile 生成的正则表达式对象,或者自定义也可
string 要匹配的字符串
findall 返回一个包含所有匹配到的字符的列表,列表类以元组的形式存在。
finditer 返回一个可迭代对象。
示例一:
pattern = re.compile(r'\d+@\w+.com') #通过 re.compile 获得一个正则表达式对象
result_finditer = re.finditer(pattern, content)
print(type(result_finditer))
print(result_finditer) # finditer 得到的结果是个可迭代对象
for i in result_finditer: # i 本身也是可迭代对象,所以下面要使用 i.group()
print(i.group())
result_findall = re.findall(pattern, content)
print(type(result_findall)) # findall 得到的是一个列表
print(result_findall)
for p in result_finditer:
print(p)
输出结果:
<class 'callable_iterator'>
<callable_iterator object at 0x10545ec88>
123456@163.com
234567@163.com
345678@163.com
<class 'list'>
['123456@163.com', '234567@163.com', '345678@163.com']
由结果可知:finditer 得到的是可迭代对象,finfdall 得到的是一个列表。
示例二:
import re
content = '''email:123456@163.com
email:234567@163.com
email:345678@163.com
'''
pattern = re.compile(r'(?P<number>\d+)@(?P<mail_type>\w+).com')
result_finditer = re.finditer(pattern, content)
print(type(result_finditer))
print(result_finditer)
iter_dict = {} # 把最后得到的结果
for i in result_finditer:
print('邮箱号码是:', i.group(1),'邮箱类型是:',i.group(2))
number = i.group(1)
mail_type = i.group(2)
iter_dict.setdefault(number, mail_type) # 使用 dict.setdefault 创建了一个字典
print(iter_dict)
print('+++++++++++++++++++++++++++++++')
result_findall = re.findall(pattern, content)
print(result_findall)
print(type(result_findall))
输出结果:
<class 'callable_iterator'>
<callable_iterator object at 0x104c5cbe0>
邮箱号码是: 123456 邮箱类型是: 163
邮箱号码是: 234567 邮箱类型是: 163
邮箱号码是: 345678 邮箱类型是: 163
{'123456': '163', '234567': '163', '345678': '163'}
+++++++++++++++++++++++++++++++
[('123456', '163'), ('234567', '163'), ('345678', '163')]
<class 'list'>
finditer 得到的可迭代对象 i,也可以使用 lastindex,lastgroup 方法。
print('lastgroup 最后一个被捕获的分组的名字',i.lastgroup)
findall 当正则没有分组,返回就是正则匹配。
re.findall(r"\d+@\w+.com", content)
['2345678@163.com', '2345678@163.com', '345678@163.com']
有一个分组返回的是分组的匹配
re.findall(r"(\d+)@\w+.com", content)
['2345678', '2345678', '345678']
多个分组时,将结果作为 元组,一并存入到 列表中。
re.findall(r"(\d+)@(\w+).com", content)
[('2345678', '163'), ('2345678', '163'), ('345678', '163')]
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/cityzenoldwang/article/details/78398406


猜你喜欢
- 本文实例为大家分享了javascript实现简单计算器的具体代码,供大家参考,具体内容如下设计一个简单的计算器代码 <body>
- 前言:本文简单讲述全文索引的应用实例,MYSQL演示版本5.5.24。Q:全文索引适用于什么场合?A:全文索引是目前实现大数据搜索的关键技术
- 1、用apt-get安装mysql#更新一下apt 仓库sudo apt-get update#安装mysql-servicesudo ap
- 使用memcache来同步session是还是不错的,当然也可以通过redis来保存session,可以php开启并将Session存储到R
- 本文主要介绍了ASP连接11种数据库的常用语法,详细内容请参考下文:1.Access数据库的DSN-less连接方法:set adocon=
- 本文实例讲述了python使用post提交数据到远程url的方法。分享给大家供大家参考。具体如下:import sys, urllib2,
- 一、破解原理其实原理很简单,一句话概括就是「大力出奇迹」,Python 有两个压缩文件库:zipfile 和 rarfile,这两个库提供的
- 在 Python 中对一个可迭代对象进行排序是很常见的一个操作,一般会用到 sorted() 函数num_list = [4, 2, 8,
- 本文中,abigale代表查询字符串,ada代表数据表名,alice代表字段名。技巧一:问题类型:ACCESS数据库字段中含有日文片假名或其
- 目录Python的安装VS Code配置Hello World测试调用Python函数string.split()第一次尝试第二次尝试第三次
- /* 判断指定的内容是否为空,若为空则弹出 警告框 */ function isEmpty(theValue, strMsg){ if(th
- 这节介绍接口测试工具postman的基本使用方法, 测试系统就是2.8节自己开发的具有用户增删改查操作的web应用程序——[FirstJav
- 这是我的xml文件结构<?xml version='1.0' encoding='utf-8'?>
- 本文实例讲述了python切片的步进、添加、连接简单操作。分享给大家供大家参考,具体如下:步进切片:#coding:utf-8a="
- 如下所示:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional
- python中的turtle库是3.6版本中新推出的绘图工具库,那么如何使用呢?下面小编给大家分享一下。首先打开pycharm软件,右键单击
- 本文实例讲述了Python只用40行代码编写的计算器。分享给大家供大家参考,具体如下:效果图:代码:from tkinter import
- 1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) sel
- display text in large ASCII art fonts 显示大ASCII艺术字体这种东西在源码声明或者软件初始化控制台打
- 经常上网的人一定碰到过找不到页面的情况,此时是否有点让人沮丧呢,本文介绍了一些404页面设计优秀的例子,当我们撞见些好玩可爱的页面时,有时反