参考这里:
Match Object Methods | Description |
---|---|
group(num=0) | This methods returns entire match (or specific subgroup num) |
groups() | This method return all matching subgroups in a tuple (empty if there weren’t any) |
知道了,原来group(0),是所有匹配的内容,而group(N)指的是原先subgroup子组对应的内容,而subgroup是原先search等规则中,用括号()所括起来的。
举例1:
#!/usr/bin/python import re line = "Cats are smarter than dogs"; matchObj = re.search( r'(.*) are(\.*)', line, re.M|re.I) if matchObj: print "matchObj.group() : ", matchObj.group() print "matchObj.group(1) : ", matchObj.group(1) print "matchObj.group(2) : ", matchObj.group(2) else: print "No match!!"
输出是:
matchObj.group(): Cats are matchObj.group(1) : Cats matchObj.group(2) :
举例2:字符串:
var pre = [false,'', '','\/recommend_music/blog/item/.html'];
然后去search:
match = re.search(r"var pre = \[(.*?),.*?,.*?,'(.*?)'\]", page, re.DOTALL | re.IGNORECASE | re.MULTILINE)print "match(0)=", match.group(0),"match(1)=",match.group(1),"match(2)=",match.group(2),"match(3)=",match.group(3)
得到的输出是:
match(0)= var pre = [false,'', '','\/recommend_music/blog/item/.html'] match(1)= false match(2)= \/recommend_music/blog/item/.html match(3)=