百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Python基础知识_正则表达式(re)

off999 2024-11-18 15:35 46 浏览 0 评论

Python 的正则表达式(Regular Expressions,简称 regex)是通过 re 模块来实现的。正则表达式是一种强大的文本处理工具,可以用来进行搜索、替换、匹配等操作。以下是一些常用的 re 模块功能及其示例:

1.导入re模块

import re

2. 编写正则表达式

正则表达式是一种模式描述语言,用于匹配字符串中的字符组合。在Python中,正则表达式通常作为字符串传递给re模块的函数。

3. 使用re模块的函数

re模块提供了多个函数来处理正则表达式,其中最常用的包括:

  • re.match(pattern, string, flags=0): 尝试从字符串的起始位置匹配正则表达式。
  • re.search(pattern, string, flags=0): 扫描字符串,返回第一个匹配正则表达式的子串的Match对象。
  • re.findall(pattern, string, flags=0): 返回字符串中所有匹配正则表达式的子串的列表。
  • re.sub(pattern, repl, string, count=0, flags=0): 使用repl替换字符串中所有匹配正则表达式的子串。
  • re.compile(pattern, flags=0): 编译一个正则表达式模式,返回一个Pattern对象。

4. 处理匹配结果

如果匹配成功,re.match()、re.search()和re.finditer()会返回一个Match对象,你可以使用这个对象来获取匹配的详细信息,比如匹配的子串、匹配的位置等。

基本匹配

1. 匹配字符串

	pattern = r'hello'  

	text = 'hello world'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())  

	else:  

	    print('No match found')

2. 匹配数字

	pattern = r'\d+'  

	text = 'There are 123 apples'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())  

	else:  

	    print('No match found')

特殊字符

1..匹配任意字符(除了换行符)


	pattern = r'a.c'  

	text = 'abc abc123'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

2.^匹配字符串的开始

python
复制代码
	pattern = r'^hello'  

	text = 'hello world'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

3.$匹配字符串的结束

python
复制代码
	pattern = r'world#39;  

	text = 'hello world'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

字符集

1.[abc]匹配方括号内的任意一个字符

python
复制代码
	pattern = r'[abc]'  

	text = 'abc def'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

2.[a-z]匹配方括号内的字符范围

python
复制代码
	pattern = r'[a-z]'  

	text = 'A quick brown fox'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

量词

1.*匹配前面的字符零次或多次

python
复制代码
	pattern = r'a*b'  

	text = 'aaab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

2.+匹配前面的字符一次或多次

python
复制代码
	pattern = r'a+b'  

	text = 'aab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

3.?匹配前面的字符零次或一次

python
复制代码
	pattern = r'a?b'  

	text = 'ab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

4.{n}匹配前面的字符恰好 n 次

python
复制代码
	pattern = r'a{3}b'  

	text = 'aaab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

分组和捕获

1. 使用圆括号进行分组

python
复制代码
	pattern = r'(\d+)-(\d+)-(\d+)'  

	text = 'My birthday is 1990-01-01'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group(0))  # 整个匹配  

	    print('Year:', match.group(1))  

	    print('Month:', match.group(2))  

	    print('Day:', match.group(3))

替换

1. 使用re.sub进行替换

python
复制代码
	pattern = r'\d+'  

	text = 'There are 123 apples and 456 oranges'  

	new_text = re.sub(pattern, 'XXX', text)  

	print(new_text)  # 输出: There are XXX apples and XXX oranges

编译正则表达式

1. 使用re.compile编译正则表达式

python
复制代码
	pattern = re.compile(r'\d+')  

	text = 'There are 123 apples'  

	match = pattern.search(text)  

	if match:  

	    print('Match found:', match.group())

查找所有匹配项

1. 使用re.findall查找所有匹配项

python
复制代码
	pattern = r'\d+'  

	text = 'There are 123 apples and 456 oranges'  

	matches = re.findall(pattern, text)  

	print(matches)  # 输出: ['123', '456']

忽略大小写

1. 使用re.IGNORECASE忽略大小写

python
复制代码
	pattern = r'hello'  

	text = 'Hello World'  

	match = re.search(pattern, text, re.IGNORECASE)  

	if match:  

	    print('Match found:', match.group())

多行匹配

1. 使用re.MULTILINE进行多行匹配

python
复制代码
	pattern = r'^hello'  

	text = '''hello  

	world  

	hello again'''  

	matches = re.findall(pattern, text, re.MULTILINE)  

	print(matches)  # 输出: ['hello', 'hello']

相关推荐

安全教育登录入口平台(安全教育登录入口平台官网)

122交通安全教育怎么登录:122交通网的注册方法是首先登录网址http://www.122.cn/,接着打开网页后,点击右上角的“个人登录”;其次进入邮箱注册,然后进入到注册页面,输入相关信息即可完...

大鱼吃小鱼经典版(大鱼吃小鱼经典版(经典版)官方版)

大鱼吃小鱼小鱼吃虾是于谦跟郭麒麟的《我的棒儿呢?》郭德纲说于思洋郭麒麟作诗的相声,最后郭麒麟做了一首,师傅躺在师母身上大鱼吃小鱼小鱼吃虾虾吃水水落石出师傅压师娘师娘压床床压地地动山摇。...

谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
哪个软件可以免费pdf转ppt(免费的pdf转ppt软件哪个好)
哪个软件可以免费pdf转ppt(免费的pdf转ppt软件哪个好)

要想将ppt免费转换为pdf的话,我们建议大家可以下一个那个wps,如果你是会员的话,可以注册为会员,这样的话,在wps里面的话,就可以免费将ppt呢转换为pdfpdf之后呢,我们就可以直接使用,不需要去直接不需要去另外保存,为什么格式转...

2026-02-04 09:03 off999

电信宽带测速官网入口(电信宽带测速官网入口app)

这个网站看看http://www.swok.cn/pcindex.jsp1.登录中国电信网上营业厅,宽带光纤,贴心服务,宽带测速2.下载第三方软件,如360等。进行在线测速进行宽带测速时,尽...

植物大战僵尸95版手机下载(植物大战僵尸95 版下载)

1可以在应用商店或者游戏平台上下载植物大战僵尸95版手机游戏。2下载教程:打开应用商店或者游戏平台,搜索“植物大战僵尸95版”,找到游戏后点击下载按钮,等待下载完成即可安装并开始游戏。3注意:确...

免费下载ppt成品的网站(ppt成品免费下载的网站有哪些)

1、Chuangkit(chuangkit.com)直达地址:chuangkit.com2、Woodo幻灯片(woodo.cn)直达链接:woodo.cn3、OfficePlus(officeplu...

2025世界杯赛程表(2025世界杯在哪个国家)

2022年卡塔尔世界杯赛程公布,全部比赛在卡塔尔境内8座球场举行,2022年,决赛阶段球队全部确定。揭幕战于当地时间11月20日19时进行,由东道主卡塔尔对阵厄瓜多尔,决赛于当地时间12月18日...

下载搜狐视频电视剧(搜狐电视剧下载安装)

搜狐视频APP下载好的视频想要导出到手机相册里方法如下1、打开手机搜狐视频软件,进入搜狐视频后我们点击右上角的“查找”,找到自已喜欢的视频。2、在“浏览器页面搜索”窗口中,输入要下载的视频的名称,然后...

pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
永久免费听歌网站(丫丫音乐网)

可以到《我爱音乐网》《好听音乐网》《一听音乐网》《YYMP3音乐网》还可以到《九天音乐网》永久免费听歌软件有酷狗音乐和天猫精灵,以前要跳舞经常要下载舞曲,我从QQ上找不到舞曲下载就从酷狗音乐上找,大多...

音乐格式转换mp3软件(音乐格式转换器免费版)

有两种方法:方法一在手机上操作:1、进入手机中的文件管理。2、在其中选择“音乐”,将显示出手机中的全部音乐。3、点击“全选”,选中所有音乐文件。4、点击屏幕右下方的省略号图标,在弹出菜单中选择“...

电子书txt下载(免费的最全的小说阅读器)

1.Z-library里面收录了近千万本电子书籍,需求量大。2.苦瓜书盘没有广告,不需要账号注册,使用起来非常简单,直接搜索预览下载即可。3.鸠摩搜书整体风格简洁清晰,书籍资源丰富。4.亚马逊图书书籍...

最好免费观看高清电影(播放免费的最好看的电影)

在目前的网上选择中,IMDb(互联网电影数据库)被认为是最全的电影网站之一。这个网站提供了各种类型的电影和电视节目的海量信息,包括剧情介绍、演员表、评价、评论等。其还提供了有关电影制作背后的详细信息,...

孤单枪手2简体中文版(孤单枪手2简体中文版官方下载)

要将《孤胆枪手2》游戏的征兵秘籍切换为中文,您可以按照以下步骤进行操作:首先,打开游戏设置选项,通常可以在游戏主菜单或游戏内部找到。然后,寻找语言选项或界面选项,点击进入。在语言选项中,选择中文作为游...

取消回复欢迎 发表评论: