使用 Python 正則表示式在給定字串中查詢所有“10+1”模式


我們需要找到給定字串中的正則表示式模式 10+1。為此,我們可以使用 Python 中提供的 re 模組。此程式包有一個名為 find all 的方法,該方法接受正則表示式和我們想要搜尋的字串。它為我們提供了該字串中模式的所有出現情況。例如,

對於輸入字串, −

10000001 hello world 10011 test100000001test.

我們應該得到輸出為 −

10000001
1001
100000001

我們可以使用 re 程式包按如下方式實現它 −

import re
occ = re.findall("10+1", "10000001 hello world 10011 test100000001test.")
for i in occ:
print(i)

這將提供輸出 −

10000001
1001
100000001


更新於: 20-Jun-2020

96 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.