python3下re模块的使用

**explain:**python3中的re库是一个正则匹配的函数库,里面包含了各种功能的正则函数,下面,我们一起学习下其中的几个常用函数

* **match()方法**:

从主串的起始位置开始匹配一个子串,匹配成功,返回匹配信息,不成功则返回NONE

print(re.match("www", "www.baidu.com"))

下面是返回的信息内容,包括子串的内容和子串在主串中的起始位置

<re.Match object; span=(0, 3), match='www'>

* **search()方法**:

从整个主串中匹配子串,与search方法相比,match方法更适合检查某个字符串的开头是否符合某个规定

print(re.search("com", "wwww.baidu.com"))

下面是返回的信息内容,与match方法一样,但如果我们在这里使用match方法则会返回NONE,因为主串的开头不是‘com’

<re.Match object; span=(11, 14), match='com'>

* **span()方法**:

只返回子串在主串中的起始位置,在match和search方法中都可使用

print(re.match('www', 'www.baidu.com').span())

下面是返回信息,是元组类型

(0, 3)

* **group()方法**:

与span方法类似,返回的是匹配的内容。

print(re.search("com", "www.baidu.com").group())

下面是返回信息

com

下面是一个运营group方法的栗子。

line = "Cats are smarter than dogs"

matchObj = re.match(r'(.*) are (.*?) .*', line, re.M | re.I)

if matchObj:

print("matchObj.group() : ", matchObj.group())#.group()方法返回匹配成功的字符串

print("matchObj.group(1) : ", matchObj.group(1))#返回第一个匹配的子分组,即为(.*)

print("matchObj.group(2) : ", matchObj.group(2))#返回第二个匹配的子分组,即为(.*?)

else:

print("No match!!")

下面是运行结果:

matchObj.group() : Cats are smarter than dogs

matchObj.group(1) : Cats

matchObj.group(2) : smarter

* **findall()方法:**

以列表的形式返回主串中所有符合规则的的子串

s = 's5s45ad465a5da'

m = r'\d+'

n = re.findall(m, s)

print(n)

下面是运行结果:

['5', '45', '465', '5'],符合规则的相邻字符被作为一个元素存到数组里。

* **finditer()方法:**

finditer找到匹配的所有的子串,并把它作为迭代器返回

s = '12 drumm44ers drumming, 11 ... 10 ...'

iter = re.finditer(r'\d+', s)

for i in iter:

print(i)

print(i.group())

print(i.span())

###上面就是一些re库中常用的正则函数,下面给大家分享两个网址,第一个是教你如何看懂复杂的正则表达式,第二个是一位大佬写的更加详细的python正则

教你看懂复杂的正则:[https://www.cnblogs.com/superstar/p/6638970.html](https://www.cnblogs.com/superstar/p/6638970.html)

详解python正则:[https://www.cnblogs.com/tina-python/p/5508402.html](https://www.cnblogs.com/tina-python/p/5508402.html)

(ps:本人太菜,若有错误的地方欢迎大佬随时责骂。。。。xixixii)