findall(pattern, string, flags=0)
在字符串string中匹配所有符合正则表达式pattern的对象,并把这些对象通过列表list的形式返回。
import re
pattern = re.compile(r'\W+')
result1 = pattern.findall('hello world!')
result2 = pattern.findall('hello world!', 0, 7)
print(result1) #[' ', '!']
print(result2) #[' ']finditer(pattern, string, flags=0)
在字符串string中匹配所有符合正则表达式pattern的对象,并把这些对象通过迭代器的形式返回。
import re
pattern = re.compile(r'\W+')
result = pattern.finditer('hello world!')
for r in result:
print(r)
#
#