findall
函數通常用于在字符串中查找所有匹配的子串,并返回一個包含所有匹配結果的列表。返回的結果是一個列表,列表中的每個元素都是匹配到的子串。
例如,在 Python 中使用 re
模塊的 findall
函數:
import re
text = "I have 3 cats and 2 dogs."
pattern = r'\d+'
matches = re.findall(pattern, text)
print(matches) # 輸出:['3', '2']
在這個例子中,findall
函數返回了一個列表 ['3', '2']
,包含了字符串中所有的數字。