python 正则 re.match_python re.search 和 re.match 正则表达式

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 18:27   41   0

python re.search 和 re.match 正则表达式

一 re.search 和 re.match

python提供了2中主要的正则表达式操作:re.match 和 re.search。

match :只从字符串的开始与正则表达式匹配,匹配成功返回matchobject,否则返回none;

search :将字符串的所有字串尝试与正则表达式匹配,如果所有的字串都没有匹配成功,返回none,否则返回matchobject;(re.search相当于perl中的默认行为)

实例代码:

import re

def testsearchandmatch():

s1="helloworld, i am 30 !"

w1 = "world"

m1 = re.search(w1, s1)

if m1:

print("find : %s" % m1.group())

if re.match(w1, s1) == none:

print("cannot match")

w2 = "helloworld"

m2 = re.match(w2, s1)

if m2:

print("match : %s" % m2.group())

testsearchandmatch()

#find : world

#cannot match

#match : helloworld

二 re.compile 和 re.ignorecase

re.compile返回regrexobject对象, 用来重复使用regrexobject;

re.ignorecase用来在匹配时忽略大小写;

实例代码:

def testcompile():

regex = "d{3}-d{7}"

regexobject = re.compile(regex)

print(regexobject.search("aaa 027-4567892").group())

print(regexobject.search("bbb 021-1234567").group())

print(regexobject.search("ccc 010-123456"))

testcompile()

#027-4567892

#021-1234567

#none

def testignorecase():

print(re.search('world', "hello world !").group())

print(re.search('world', "hello world !", re.ignorecase).group())

print(re.search('world', "hello world !"))

testignorecase()

#world

#world

#none

三 matchobject

matchobject为re.search,re.match等匹配成功后返回的对象,包含了匹配的结果。

在正则表达式中,可以使用()来将部分正则表达式分组且编号,编号从1开始,使用数字来使用,例如1 2 3,(?p)还可以给分组命名, 使用(?p=name)来使用命名的组。

matchobject.group()包含了所有匹配的内容,等价于matchobject.group(0),此时的0表示所有的匹配;

matchobject.groups教程()包含了正则表达式中所有使用()定义的组对应的匹配内容;

matchobject.group(n),表示返回正则表达式中的第n个组()匹配的内容,此时n不为0, 等价于matchobject.groups()[n-1];

matchobject.lastindex, 表示正则表达式中分组()的个数;

实例代码:

def testmatchobject():

m = re.match(r"(?pd{4})-(?pd{2})-(?pd{2})", "2010-10-01, i am very happy")

print(m.group())

print(m.group(0))

print(m.groups())

print(m.group(1))

print(m.groups()[0])

print(m.group(2))

print(m.groups()[1])

print(m.group(3))

print(m.groups()[2])

print(m.groupdict())

print(m.lastindex)

testmatchobject()

#2010-10-01

#2010-10-01

#('2010', '10', '01')

#2010

#2010

#10

#10

#01

#01

#{'date': '01', 'year': '2010', 'month': '10'}

#3

四 re和matchobject的方法split+findall+finditer+sub

split方法,使用给定的表达式来分割字符串;

findall方法,返回所有的与给定的表达式匹配的一个list;

finditer方法,返回所有与给定的表达式匹配的matchobject的iterator;

sub方法,使用新的字符串替换表达式匹配的字符串;

实例代码:

def testreandmatchobjectmethonds():

#split findall finditer sub

s1 = "i am working in microsoft !"

l = re.split('s+', s1) # l is list type

print( l)

s2 = "aa 12 bb 3 cc 45 dd 88 gg 89"

l2 = re.findall('d+', s2)

print(l2)

it = re.finditer('d+', s2) # it is one iterator type

for i in it: # i is matchobject type

print (i.group())

s3 = re.sub('d+', '200', s2)

print(s3)

testreandmatchobjectmethonds()

#['i', 'am', 'working', 'in', 'microsoft', '!']

#['12', '3', '45', '88', '89']

#12

#3

#45

#88

#89

#aa 200 bb 200 cc 200 dd 200 gg 200

五 re.search只返回第一个匹配

实例代码:

def testsearch():

s1 = "bb 3 cc 45 dd 88 gg 89"

m = re.search('d+', s1)

print(m.group())

testsearch()

#3

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP