python正则表达式的学习记录

match和findall的区别以及有括号和无括号的区别

strvar = "hello

world"
find_re = re.compile("hello[.|
]*([S]*)");
rs = find_re.match(strvar);

print rs.groups()
#输出('world',) rs
= find_re.findall(strvar); for x in rs: print x;
#输出world

match是查找到匹配的字符串,也就是括号内的字符串,match应该是从一个字符串里面过滤出我们括号内需要的字符串,所以准确来讲match更像是过滤操作

findall则是从一个字符串里面查找到对应的字符串,直到遇见换行符或者结束符 

findall会查找所有匹配的文本,当找到第一个匹配的后,会从第一个匹配的字符串结束位置开始查找

如果上面我们在compile的时候把[.| ]改成[.],那么正则只会查找到hello 就停止,这点上感觉有点类似于sed了

原文地址:https://www.cnblogs.com/linyilong3/p/3354745.html