您的位置:首页 > 编程语言 > Python开发

HTML解析之二:python与正则表达式

2017-10-21 22:27 531 查看
#coding:utf8

# re中的compile函数,将一个正则表达式的字符串转化为pattern匹配对象
# 如  pattern = re.compile(r'\d+')
# 生成一个匹配数字的pattern对象,给接下来的函数做参数

#一:re.match(pattern,string[,flags])
#从输入参数string(匹配的字符串)开头开始,尝试匹配pattern,一直向后匹配,
#若遇到无法匹配的字符串或已经到达string的末尾,立即返回None,反之获取匹配结果
import re
#将正则表达式编译成pattern对象
pattern = re.compile(r'\d+')
#使用re.match匹配文本,获得匹配结果,无法匹配时将返回None
result1 = re.match(pattern,'192abc')
if result1:
print result1.group()
else:
print '匹配失败1'
result2 = re.match(pattern,'abc192')
if result2:
print result2.group()
else:
print '匹配失败2'
#匹配192abc字符串时,match函数是从字符串开头开始匹配,匹配到192立即返回值,
#通过group()可以获取捕捉的值
#同样,匹配abc192字符串时,字符串开头不符合正则表达式,立即返回None

#二:re.search(pattern,string[,flags])
#match()函数只从string的开始位置匹配,search()会扫描整个string查找匹配;
#match()只有在string起始位置匹配成功的时候才有返回,若不是在起始位置匹配成功,match()就返回None;
#search()方法的返回对象和match()返回对象在方法和属性上是一致的
import re
#将正则表达式编译成pattern对象
pattern = re.compile(r'\d+')
#使用re.match匹配文本获得匹配结果,无法匹配时讲返回None
result1 = re.search(pattern,'abc192edf')
if result1:
print result1.group()
else:
print '匹配失败1'
#三:re.split(pattern,string[,maxsplit])
#按照能够匹配的字符串将string分割后返回列表。maxsplit用于指定最大分割次数,不指定时将全部分割。
import re
pattern = re.compile(r'\d+')
print re.split(pattern,'A1B2C3D4')

#四:re.findall(patern,string[,flags])
#搜索整个string,以列表形式返回能匹配的全部子串
import re
pattern = re.compile(r'\d+')
print re.findall(pattern,'A1B2C3D4')

#五:re.finditer(pattern,string[,flags])
#搜索整个string,以迭代器形式返回能匹配的全部Match对象。
import re
pattern = re.compile(r'\d+')
matchiter = re.finditer(pattern,'A1B2C3D4')
for match in matchiter:
print match.group()

#六:re.sub(pattern,repl,string[,count])
#使用repl替换string中每一个匹配的子串后返回替换后的字符串;
#当repl是一个字符串时,可以用\id或\g<id>,\g<name>引用分组,但不能使用编号0;
#当repl是一个方法时,这个方法应当只接受一个参数(Match对象),并返回一个字符串用于替换(返回的字符串中不能再引用分组)。
#count用于指定最多替换次数,不指定时全部替换
import re
p = re.compile(r'(?P<word1>\w+) (?P<word2>\w+)') #使用名称引用
s = 'i say, hello world!'
print p.sub(r'\g<word2> \g<word1>', s)
p = re.compile(r'(\w+) (\w+)') #使用编号
print p.sub(r'\2 \1', s)
def func(m):
return m.group(1).title() + ' ' + m.group(2).title()
print p.sub(func, s)

#七:re.subn(pattern,repl,string[,count])
#返回(sub(repl,string[,count]), 替换次数)
import re
s = 'i say, hello world!'
p = re.compile(r'(\w+) (\w+)')
print p.subn(r'\2 \1', s)
def func(m):
return m.group(1).title() + ' ' + m.group(2).title()
print p.subn(func, s)

以上7个函数在re模块中进行索搜匹配,将捕捉到的值提取出来就需要用到Match对象,
之前已经使用了Match中的groups方法,现在介绍一下Match对象的属性和方法

Match对象的属性:
string:匹配时使用的文本;
re:匹配时使用的Pattern对象;
pos:文本中正则表达式开始搜索的索引。值与Pattern.match()和Pattern.search()方法的同名参数相同;
endpos:文本中正则表达式结束搜索的索引。值与Pattern.match()和Pattern.search()方法的同名参数相同;
lastindex:最后一个被捕获的分组在文本中的索引。如果没有被捕获的分组,将为None;
lastgroup:最后一个被捕获的分组的别名。如果这个分组没有别名或者没有被捕获的分组,将为None;

Match对象的属性:
group([group1,...]):获得一个或多个分组截获的字符串,指定多个参数时将以元组形式返回。
group1可以使用编号也可以使用别名,编号0代表整个匹配的子串,不填写参数时,返回group(0).
没有解惑字符串的组返回None,截获了多次的组返回最后一次截获的子串。
groups([default]):以元组形式返回全部分组截获的字符串。相当于调用group(1,2,...last).
default表示没有截获字符串的组以这个值替代,默认为None。
groupdict([default]):返回以有别名的组的别名为键,以该组截获的子串为值的字典,没有别名的组不包含在内。default含义同上;
start([group]):返回指定的组截获的子串在string中的起始索引(子串第一个字符的索引).group默认值为0.
end([group]):返回指定的组截获的子串在string中的结束索引(子串最后一个字符的索引+1).group默认值为0。
span([group]):返回(start(group),end(group)).
expand(template):将匹配到的分组代入template中然后返回。template中可以使用\id或\g<id>、\g<name>引用分组,但不能使用编号0.
\id与\g<id>是等价的,但\10将被认为是第10个分组,如果你想表达\1之后是字符'0',只能使用\g<1>0
示例如下:

import re
pattern = re.compile(r'(\w+) (\w+) (?P<word>.*)')
match = pattern.match('I love you!')

print "match.string:",match.string
print "match.re:",match.re
print "match.pos:",match.pos
print "match.endpos:",match.endpos
print "match.lastindex:",match.lastindex
print "match.lastgroup:",match.lastgroup

print "match.group(1,2):",match.group(1,2)
print "match.groups():",match.groups()
print "match.groupdict():",match.groupdict()
print "match.start(2):",match.start(2)
print "match.end(2):",match.end(2)
print "match.span(2):",match.span(2)
print r"match.expand(r'\2 \1 \3'):", match.expand(r'\2 \1 \3')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: