如何編寫 Python 正則表示式來獲得模式內的零個或多個重複?\n\n
* 正則表示式中的星號元字元 表示其左側的模式出現 0 次或多次
以下程式碼匹配並列印字串“chihua huahua”中模式“\w”的 0 次或多次出現
範例
import re s = 'chihua huahua' result = re.findall(r'\w*', s) print result
輸出
輸出如下所示
['chihua', '', 'huahua', '']
廣告
* 正則表示式中的星號元字元 表示其左側的模式出現 0 次或多次
以下程式碼匹配並列印字串“chihua huahua”中模式“\w”的 0 次或多次出現
import re s = 'chihua huahua' result = re.findall(r'\w*', s) print result
輸出如下所示
['chihua', '', 'huahua', '']