python基础-正则表达式-笔记1

match()方法匹配字符串与 search()方法匹配字符串的区别:

match()函数试图从字符串的起始部分对模式进行匹配,

模式“foo”将在字符串“food on the table”中找到一个匹配,因为它是从字符串的起 始部分进行匹配的。

模式“foo”不能在字符串“ on food on the table”中找到一个匹配,因为它是从字符串的起 始部分进行匹配的。

search()在任意位置对给定正则表达式模式搜索第一次出现 的匹配情况

# -*- coding:utf-8 -*-
import re
m=re.match('(\w\w\w)-(\d\d\d)','abc-123')
print(m.group(),"完整匹配")
print(m.group(1),'子组1')
print(m.group(2),'子组2')
print(m.groups(),'全部子组')

猜你喜欢

转载自blog.csdn.net/qq_39208536/article/details/81196349