1.re的split,findall,finditer方法

正则对象的split方法

split(string[,maxsplit])

按照能够匹配的字符串将string分割后返回列表.maxsplit用于指定最大分割次数,不指定将全部分割.

import re
p=re.compile(r'\d+')
print(p.split('one1two2three3four4'))

 

正则对象的findall方法

findall(string[,pos[,endpos]])

搜索string,以列表形式返回全部能匹配的字符串;

import re
p=re.compile(r'\d+')
print(p.findall('one1two2three3four4'))for i in p.finditer('one1two2three3four4'):
	print(i.group())

2.re的match对象
match匹配对象:
Match对象是一次匹配的结果,包含了很多关于此次匹配的信息,可以使用Match提供的可读属性或方法来获取这些信息.上面的过程中多次使用了match对象,调用了他的group()和groups()等方法;

import re
prog = re.compile(r'(?P<tagname>abc)(\w*)(?P=tagname)')
result=prog.match('abcliuijde4543abc')
print(result.groups())
print(result.group('tagname'))
print(result.group(2))
print(result.groupdict())#match对象的group返回一个元组,下标是以1开头;

 3.re的matche方法和search方法

match(string[,pos[,endpos]])
string:匹配使用的文本
pos:文本中正则表达式开始搜索的索引,及开始搜索string的下标;
endpos:文本中正则表达式结束搜索的索引.

如果不指定pos,默认是从开头开始匹配,如果匹配不到,直接返回None.

import re
pattern=re.compile(r'(hello w.*)(hello l.*)')
result=pattern.match(r'aahello world hello ling')
print(result)
result2=pattern.match(r'hello world hello ling')
print(result2.groups())#search
result3=reg.search(b)
print(result3)