几分钟带你学会python中的re模块和正则表达式

re模块是Python中用于处理正则表达式的标准库,它提供了一组函数和类来进行字符串匹配、替换和分割等操作。本文将详细介绍re模块中常用的功能和用法。

正则表达式语法

正则表达式是一种特殊的字符串模式,用于描述文本中的某种规律或模式。re模块支持大部分正则表达式语法,下面是一些常用的语法元字符:

元字符描述
.匹配除了换行符以外的任意一个字符
^匹配字符串的起始位置
$匹配字符串的结束位置
*匹配前面的字符零次或多次
+匹配前面的字符一次或多次
?匹配前面的字符零次或一次
{m}匹配前面的字符恰好m次
{m,}匹配前面的字符至少m次
{m,n}匹配前面的字符至少m次,至多n次
[...]匹配中括号中任意一个字符
[^...]匹配除中括号中字符外的任意一个字符
(...)匹配括号中的正则表达式
``
\d匹配任意一个数字字符,等价于[0-9]
\D匹配任意一个非数字字符,等价于[^0-9]
\w匹配任意一个字母、数字或下划线字符,等价于[A-Za-z0-9_]
\W匹配任意一个非字母、数字或下划线字符,等价于[^A-Za-z0-9_]
\s匹配任意一个空白字符,包括空格、制表符、换行符等
\S匹配任意一个非空白字符

re.match()函数

re.match(pattern, string, flags=0)函数用于从字符串的起始位置开始匹配一个模式。如果匹配成功,则返回一个匹配对象(Match object),否则返回None。

import re

string = 'HELLO\nworld'
pattern = r'hello'

match_obj = re.search(pattern, string, re.IGNORECASE)
if match_obj:
    print("Match found:", match_obj.group())
else:
    print("No match")

re.search()函数

re.search(pattern, string, flags=0)函数用于在字符串中搜索与模式匹配的子串。如果找到第一个匹配项,则返回一个匹配对象(Match object),否则返回None。

import re

pattern = r'world'
string = 'hello world'

match_obj = re.search(pattern, string)
if match_obj:
    print("Match found:", match_obj.group())
else:
    print("No match")

re.findall()函数

re.findall(pattern, string, flags=0)函数用于在字符串中查找所有匹配模式的子串,并以列表的形式返回。

import re

pattern = r'\d+'
string = 'There are 12 apples and 15 bananas'

match_list = re.findall(pattern, string)
print(match_list)

re.sub()函数

re.sub(pattern, repl, string, count=0, flags=0)函数用于用指定的字符串替换匹配到的模式。可以使用count参数来指定最多替换的次数,默认为全部替换。

import re

pattern = r'apple'
string = 'I have an apple and a banana'

new_string = re.sub(pattern, 'orange', string)
print(new_string)

re.split()函数

re.split(pattern, string, maxsplit=0, flags=0)函数根据模式分割字符串,并返回分割后的子串列表。可以使用maxsplit参数来限制最多分割的次数。

import re

pattern = r'\s+'
string = 'hello world'

split_list = re.split(pattern, string)
print(split_list)

['hello', 'world']

re.compile()函数

re.compile(pattern, flags=0)函数将正则表达式编译成Pattern对象,可以多次使用。

import re

pattern = r'\d+'
string = 'There are 12 apples and 15 bananas'

regex = re.compile(pattern)
match_list = regex.findall(string)
print(match_list)

['12', '15']

Match对象方法

当使用re.match()re.search()成功匹配后,返回的是一个Match对象。Match对象具有以下方法:

方法描述
group([group1, ...])返回整个匹配对象或指定分组的匹配部分
start([group])返回指定分组或整个匹配部分在原字符串中的起始位置
end([group])返回指定分组或整个匹配部分在原字符串中的结束位置
span([group])返回指定分组或整个匹配部分在原字符串中的起始和结束位置的元组
import re

pattern = r'(\d+)-(\d+)'
string = '2022-12'

match_obj = re.match(pattern, string)
if match_obj:
    print("Full match:", match_obj.group())
    print("First group:", match_obj.group(1))
    print("Second group:", match_obj.group(2))
    print("Start position:", match_obj.start())
    print("End position:", match_obj.end())
    print("Start and end positions:", match_obj.span())

flags参数

re模块中有一些可选的标志参数,可以用来控制匹配的方式。下面是一些常用的标志参数:

标志参数描述
re.IGNORECASE 或 re.I忽略大小写匹配
re.MULTILINE 或 re.M多行匹配
re.DOTALL 或 re.S匹配任意字符,包括换行符
re.ASCII使\w\W\b\B\d\D\s\S仅匹配ASCII字符
re.UNICODE 或 re.U使\w\W\b\B\d\D\s\S匹配所有Unicode字符
import re

string = 'HELLO\nworld'
pattern = r'hello'

match_obj = re.search(pattern, string, re.IGNORECASE)
if match_obj:
    print("Match found:", match_obj.group())
else:
    print("No match")

  • 23
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值