Python3之正则表达式

本文详细介绍了Python3中的正则表达式,包括re.match、re.search、re.sub、re.compile、re.findall、re.finditer、re.split等函数的使用,以及正则表达式模式、修饰符和括号的应用。文章通过实例解析了各种方法的功能和操作,帮助读者深入理解正则表达式在Python中的应用。
摘要由CSDN通过智能技术生成

一、re.match函数

re.match()函数从字符串的起始位置匹配一个模式,若起始位置匹配成功返回匹配对象,若匹配失败则返回none

1、语法

re.match(pattern , string , flags = 0)

2、参数

参数 说明
pattern 匹配的正则表达式
string 要匹配的字符串
flags 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等,参考:正则表达式修饰符 - 可选标志

3、re.match()返回对象处理

re.match()函数可使用如下方法处理返回对象

方法名称 作用说明
start() 返回匹配开始位置
end() 返回匹配结束位置
span() 以元组tuple形式返回匹配的范围
group() 以str形式返回对象中match的元素

注意:以上方法不适用于re.findall()函数,因为re.findall()返回的是一个列表list
注意:当匹配结果为none时,无法使用start、end、span和group方法,报
AttributeError: ‘NoneType’ object has no attribute 'start’错误

# re.match()函数

import re

restr = "kobe"

str1 = "there is a player , his name is koke , kobe is a An American , kobe is still a legend ; I miss you , kobe"

str2 = "kobe is a An American , kobe is still a legend ; I miss you , kobe"

matchstr1 = re.match(restr , str1)
matchstr2 = re.match(restr , str2)

print("matchstr1:" , matchstr1)
print("matchstr2:" , matchstr2)

print("matchstr2.start():" , matchstr2.start())
print("matchstr2.end():" , matchstr2.end())
print("matchstr2.span():" , matchstr2.span())
print("matchstr2.group():" , matchstr2.group())
print("type(matchstr2.group()):" , type(matchstr2.group()))
print("matchstr1.start():" , matchstr1.start())
matchstr1: None
matchstr2: <re.Match object; span=(0, 4), match='kobe'>
matchstr2.start(): 0
matchstr2.end(): 4
matchstr2.span(): (0, 4)
matchstr2.group(): kobe
type(matchstr2.group()): <class 'str'>
Traceback (most recent call last):
  File "G:/ZX_01_正则表达式_match函数.py", line 22, in <module>
    print("matchstr1.start():" , matchstr1.start())
AttributeError: 'NoneType' object has no attribute 'start'

4、group()方法和groups()方法

4.1、group()方法

正则表达式中,group()方法用来提取分组截获的字符串,其中()用来分组。
组是通过 “(” 和 “)” 元字符来标识的,它们一起把在它们里面的表达式组成一组。例如可以用重复限制符,如 、 +、?、{m,n}来重复组里的内容,比如说(ab) 将匹配零或更多个重复的 “ab”。
如果不引入括号,将整个表达式作为一个组,是group(0)。
一般,re.match().group(N) 代表返回第N组括号匹配的字符,当re.match().group(M)中M不存在时,报错。
re.match().group() == re.match().group(0) == 所有匹配的字符,与括号无关。

group(0)匹配的是整个表达式的字符串,group() 可以一次输入多个组号,在这种情况下其返回一个包含那些组所对应值的元组。

# 1、group()方法
import re
str = "123abc456def"
restr = "([0-9]*)([a-zA-Z]*)([0-9]*)([a-zA-Z]*)" # 匹配类似“1a2”字符串
match = re.match(restr , str)
print("match.group():" , match.group()) # group()同group(0),匹配正则表达式整体结果
print("match.group(0):" , match.group(0)) # group()同group(0),匹配正则表达式整体结果
print("match.group(1):" , match.group(1)) # 匹配group(1)结果
print("match.group(2):" , match.group(2)) # 匹配group(2)结果
print("match.group(3):" , match.group(3)) # 匹配group(3)结果
print("match.group(4):" , match.group(4)) # 匹配group(4)结果
print("match.group(5):" , match.group(5)) # group(5)不存在报错
match.group(): 123abc456def
match.group(0): 123abc456def
match.group(1): 123
match.group(2): abc
match.group(3): 456
match.group(4): def
Traceback (most recent call last):
  File "G:/group和groups方法.py", line 14, in <module>
    print("match.group(5):" , match.group(5))
IndexError: no such group
4.2、groups()方法

re.match().groups() 返回所有括号匹配的字符,以tuple格式(元组格式),不包括re.match().group(0),即整个表达式。
re.match().groups() == (re.match().group(0), re.match().group(1), …)

# 2、groups()方法
import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d\d\d)-(\d\d\d)')
str = '021-12345-666 is My phone number .'
match = re.match(phoneNumRegex , str)
print("match.group(0):" , match.group(0))
print("match.group(1):" , match.group(1))
print("match.group(2):" , match.group(2))
print(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值