輸入是我正在使用的代碼,輸出是我得到的輸出,所需的輸出是我想要的輸出。
輸入:
import regex as re
keyword = 'Auto: tab suspender 2.0 pro'
keyword = re.sub(r'[^\w\s]','', keyword)
words = re.findall('\w ', keyword)
print(keyword)
print(len(words))
words
輸出:
Auto tab suspender 20 pro
5
['Auto', 'tab', 'suspender', '20', 'pro']
所需輸出:
Auto tab suspender 2.0 pro
5
['Auto', 'tab', 'suspender', '2.0', 'pro']
uj5u.com熱心網友回復:
我會re.findall在這里使用:
keyword = 'Auto: tab suspender 2.0 pro'
matches = re.findall(r'\d (?:\.\d )?|\w ', keyword)
print(matches) # ['Auto', 'tab', 'suspender', '2.0', 'pro']
此處使用的正則運算式模式首先嘗試匹配整數或浮點數,失敗將查找單詞:
\d匹配一個整數(?:\.\d )?或者可能是一個浮動|或者\w匹配一個詞
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/527822.html
