当前位置: 首页 > news >正文

正则表达式篇

文章目录

  • 1. 导入re模块
  • 2. 正则表达式的基本模式
  • 3. re模块的主要函数和方法
  • 4. 示例

正则表达式(Regular Expression,常简写为regex或regexp)是一种强大的文本处理工具,它使用一种特殊的字符序列来帮助用户检查一个字符串是否与某种模式匹配。Python内置的re模块提供了完整的正则表达式功能。

以下是一个关于Python中正则表达式的详细教程:

1. 导入re模块

首先,你需要导入Python的re模块来使用正则表达式。


import re

2. 正则表达式的基本模式

  • 字符匹配:
    • .:匹配任意字符(除了换行符)
    • [abc]:匹配方括号中的任意一个字符
    • [^abc]:匹配不在方括号中的任意一个字符
    • [a-z]:匹配任意小写字母
    • [A-Z]:匹配任意大写字母
    • [a-zA-Z]:匹配任意字母
    • [0-9]:匹配任意数字
    • \d:匹配任意数字,等同于[0-9]
    • \D:匹配非数字,等同于[^0-9]
    • \w:匹配任意字母、数字或下划线,等同于[a-zA-Z0-9_]
    • \W:匹配非字母、数字或下划线,等同于[^a-zA-Z0-9_]
    • \s:匹配任意空白字符,包括空格、制表符、换页符等
    • \S:匹配非空白字符
  • 数量词:
    • *:匹配前面的子表达式零次或多次
    • +:匹配前面的子表达式一次或多次
    • ?:匹配前面的子表达式零次或一次
    • {n}:匹配前面的子表达式恰好n次
    • {n,}:匹配前面的子表达式至少n次
    • {n,m}:匹配前面的子表达式至少n次,但不超过m次
  • 边界匹配:
    • ^:匹配字符串的开始
    • $:匹配字符串的结束
    • \b:匹配一个单词边界
    • \B:匹配非单词边界
  • 选择、分组和引用:
    • |:或操作,匹配|左右任意一个表达式
    • ():捕获括号,对正则表达式进行分组,并捕获匹配的文本
    • (?:…):非捕获括号,只进行分组,不捕获匹配的文本
    • \n:引用前面第n个捕获括号中匹配的文本(n为正整数)
      转义字符:
    • \:对特殊字符进行转义,使其失去特殊意义

3. re模块的主要函数和方法

  • re.match(pattern, string, flags=0):从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回None。
  • re.search(pattern, string, flags=0):扫描整个字符串并返回第一个成功的匹配。
  • re.findall(pattern, string, flags=0):在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
  • re.finditer(pattern, string, flags=0):和findall类似,但返回的是一个迭代器。
  • re.split(pattern, string, maxsplit=0, flags=0):按照能够匹配的子串将字符串分割后返回列表。
  • re.sub(pattern, repl, string, count=0, flags=0):在字符串中查找匹配正则表达式的部分,并将其替换为另一个字符串。
  • re.compile(pattern, flags=0):将正则表达式编译成一个Pattern对象,可以供match()和search()这两个函数使用。

4. 示例

以下是一些使用Python正则表达式的示例:

  1. re.match()

import re  # 匹配字符串起始位置的模式  
pattern = r'Hello'  
string = 'Hello, world!'  
match = re.match(pattern, string)  
if match:  print('Found match:', match.group())  # 输出: Found match: Hello  
else:  print('No match found.')
  1. re.search()

import re  # 在整个字符串中搜索模式  
pattern = r'\d+'  # 匹配一个或多个数字  
string = 'The price is 123 dollars.'  
search = re.search(pattern, string)  
if search:  print('Found match:', search.group())  # 输出: Found match: 123  
else:  print('No match found.')
  1. re.findall()

import re  # 找到所有匹配模式的子串  
pattern = r'\b\w+\b'  # 匹配单词边界之间的单词  
string = 'Hello world, this is a Python tutorial.'  
matches = re.findall(pattern, string)  print('Matches:', matches)  # 输出: Matches: ['Hello', 'world', 'this', 'is', 'a', 'Python', 'tutorial']
  1. re.finditer()

import re  # 找到所有匹配模式的子串,并返回迭代器  
pattern = r'\d+'  
string = 'The numbers are 123 and 456.'  
matches = re.finditer(pattern, string)  
for match in matches:  print('Found match:', match.group())  # 输出: Found match: 123 和 Found match: 456
  1. re.split()

import re  # 使用模式分割字符串  
pattern = r'\s+'  # 匹配一个或多个空白字符  
string = 'This is a test string.'  
split_string = re.split(pattern, string)  print('Split string:', split_string)  # 输出: Split string: ['This', 'is', 'a', 'test', 'string.']
  1. re.sub()

import re  # 替换字符串中匹配模式的子串  
pattern = r'\d+'  
repl = 'NUMBER'  
string = 'The price is 123 dollars and the code is 456.'  
new_string = re.sub(pattern, repl, string)  print('New string:', new_string)  # 输出: New string: The price is NUMBER dollars and the code is NUMBER.
  1. re.compile()

import re  # 编译正则表达式为Pattern对象,之后可以多次使用  
pattern = re.compile(r'\b\w+\b')  
string = 'Hello world, this is a Python tutorial.'  
matches = pattern.findall(string)  print('Matches:', matches)  # 输出: Matches: ['Hello', 'world', 'this', 'is', 'a', 'Python', 'tutorial']

这些例子涵盖了re模块中常用的函数和方法,并展示了如何使用它们来匹配、搜索、查找所有匹配项、迭代匹配项、分割字符串以及替换字符串中的模式。你可以根据实际需要调整正则表达式和字符串来适应不同的场景。

http://www.lryc.cn/news/327044.html

相关文章:

  • CAST(columnA AS VARCHAR(255)) AS fieldA报错的问题
  • github加速神器!解决github巨慢的问题,并且能够加速下载!另外推荐GitKraken -- 超好用的 Git 可视化工具
  • Stable Diffusion XL之使用Stable Diffusion XL训练自己的AI绘画模型
  • 软件杯 深度学习 机器视觉 人脸识别系统 - opencv python
  • IDEA | 资源文件中文乱码问题解决
  • Linux系统使用Docker部署Portainer结合内网穿透实现远程管理容器和镜像
  • 【Git篇】复习git
  • [LitCTF 2023]程序和人有一个能跑就行了
  • 如何在群晖NAS搭建bitwarden密码管理软件并实现无公网IP远程访问
  • perl:获取同花顺数据--业绩快报,业绩公告
  • FPGA选型
  • centos系统的root密码忘记或失效的解决办法(超详细)
  • 【Android 源码】Android源码下载指南
  • MySQL数据库高级语句
  • 软件测试【理论基础】
  • 蓝桥杯每日一题(floyd算法)
  • 文心一言 VS 讯飞星火 VS chatgpt (224)-- 算法导论16.3 6题
  • flutter3_douyin:基于flutter3+dart3短视频直播实例|Flutter3.x仿抖音
  • VR全景赋能智慧农业,打造沉浸式种植体验平台
  • 百度文心一言(ERNIE bot)API接入Android应用
  • springboot基本使用八(mbatisplus+filter实现登录功能)
  • 蚂蚁庄园今天答案
  • 第5章 数据建模和设计
  • 牛客NC108 最大正方形【中等 动态规划 Java,Go,PHP】
  • C#学生信息成绩管理系统
  • 精品凉拌菜系列热卤系列课程
  • Java代码基础算法练习-求一个三位数的各位数字之和-2024.03.27
  • Excel 十字交叉聚光灯查询,再也不用担心看串行与列
  • 集合和字符串的使用
  • Wagtail-基于Python Django的内容管理系统CMS实现公网访问