extractAll在Python中如何使用

小樊
90
2024-07-11 00:20:34

在Python中,我們可以使用re模塊的findall()函數(shù)來(lái)提取所有匹配的字符串。下面是一個(gè)簡(jiǎn)單的示例,展示如何使用findall()函數(shù)來(lái)提取所有匹配的字符串:

import re

text = "The quick brown fox jumps over the lazy dog"
matches = re.findall(r'\b\w{3,}\b', text)

for match in matches:
    print(match)

在這個(gè)示例中,我們使用正則表達(dá)式’\b\w{3,}\b’來(lái)匹配所有長(zhǎng)度大于等于3的單詞。然后,我們使用re.findall()函數(shù)來(lái)提取所有匹配的單詞,并將它們打印出來(lái)。

運(yùn)行這段代碼,會(huì)輸出以下結(jié)果:

The
quick
brown
fox
jumps
over
the
lazy
dog

0