(1)re.match()总是从字符串“开头”去匹配,并返回匹配的字符串的match对象;所以当你用re.match()去匹配字符串非开头部分的字符串时,会返回NONE,例子如下所示; str1='Hello World!' print(re.match(r'e',str1)) 因此,如果想查找字符串任意部分的出现位置时用re.search()或re.findall(); (2)re.search()函数是对整个字符串进行搜索,并返回第一个匹配的字符串的match对象; str1='Hello World!' print(re.search(r'e',str1)) (3)re.findall()函数将返回一个所有匹配的字符串的字符串列表; str1='Hello World!' print(re.findall(r'o',str1))
|
|