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

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

off999 2024-11-18 15:35 31 浏览 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']

相关推荐

在NAS实现直链访问_如何访问nas存储数据

平常在使用IPTV或者TVBOX时,经常自己会自定义一些源。如何直链的方式引用这些自定义的源呢?本人基于armbian和CasaOS来创作。使用标准的Web服务器(如Nginx或Apache...

PHP开发者必备的Linux权限核心指南

本文旨在帮助PHP开发者彻底理解并解决在Linux服务器上部署应用时遇到的权限问题(如Permissiondenied)。核心在于理解“哪个用户(进程)在访问哪个文件(目录)”。一、核心...

【Linux高手必修课】吃透sed命令!文本手术刀让你秒变运维大神!

为什么说sed是Linux运维的"核武器"?想象你有10万个配置文件需要批量修改?传统方式要写10万行脚本?sed一个命令就能搞定!这正是运维工程师的"暴力美学"时...

「实战」docker-compose 编排 多个docker 组成一个集群并做负载

本文目标docker-compose,对springboot应用进行一个集群(2个docker,多个类似,只要在docker-compose.yml再加boot应用的服务即可)发布的过程架构...

企业安全访问网关:ZeroNews反向代理

“我们需要让外包团队访问测试环境,但不想让他们看到我们的财务系统。”“审计要求我们必须记录所有第三方对内部系统的访问,现在的VPN日志一团糟。”“每次有新员工入职或合作伙伴接入,IT部门都要花半天时间...

反向代理以及其使用场景_反向代理实现过程

一、反向代理概念反向代理(ReverseProxy)是一种服务器配置,它将客户端的请求转发给内部的另一台或多台服务器处理,然后将响应返回给客户端。与正向代理(ForwardProxy)不同,正向代...

Nginx反向代理有多牛?一篇文章带你彻底搞懂!

你以为Nginx只是个简单的Web服务器?那可就大错特错了!这个看似普通的开源软件,实际上隐藏着惊人的能力。今天我们就来揭开它最强大的功能之一——反向代理的神秘面纱。反向代理到底是什么鬼?想象一下你...

Nginx反向代理最全详解(原理+应用+案例)

Nginx反向代理在大型网站有非常广泛的使用,下面我就重点来详解Nginx反向代理@mikechen文章来源:mikechen.cc正向代理要理解清楚反向代理,首先:你需要搞懂什么是正向代理。正向代理...

centos 生产环境安装 nginx,包含各种模块http3

企业级生产环境Nginx全模块构建的大部分功能,包括HTTP/2、HTTP/3、流媒体、SSL、缓存清理、负载均衡、DAV扩展、替换过滤、静态压缩等。下面我给出一个完整的生产环境安装流程(C...

Nginx的负载均衡方式有哪些?_nginx负载均衡机制

1.轮询(默认)2.加权轮询3.ip_hash4.least_conn5.fair(最小响应时间)--第三方6.url_hash--第三方...

Nginx百万并发优化:如何提升100倍性能!

关注△mikechen△,十余年BAT架构经验倾囊相授!大家好,我是mikechen。Nginx是大型架构的核心,下面我重点详解Nginx百万并发优化@mikechen文章来源:mikechen....

在 Red Hat Linux 上搭建高可用 Nginx + Keepalived 负载均衡集群

一、前言在现代生产环境中,负载均衡是确保系统高可用性和可扩展性的核心技术。Nginx作为轻量级高性能Web服务器,与Keepalived结合,可轻松实现高可用负载均衡集群(HA+LB...

云原生(十五) | Kubernetes 篇之深入了解 Pod

深入了解Pod一、什么是PodPod是一组(一个或多个)容器(docker容器)的集合(就像在豌豆荚中);这些容器共享存储、网络、以及怎样运行这些容器的声明。我们一般不直接创建Pod,而是...

云原生(十七) | Kubernetes 篇之深入了解 Deployment

深入了解Deployment一、什么是Deployment一个Deployment为Pods和ReplicaSets提供声明式的更新能力。你负责描述Deployment中的目标状...

深入理解令牌桶算法:实现分布式系统高效限流的秘籍

在高并发系统中,“限流”是保障服务稳定的核心手段——当请求量超过系统承载能力时,合理的限流策略能避免服务过载崩溃。令牌桶算法(TokenBucket)作为最经典的限流算法之一,既能控制请求的平...

取消回复欢迎 发表评论: