我有一個字串 誰做了什么?只有當 DID 是大寫字母時,我才能在下面的正則運算式中添加什么來搜索?
output = re.findall(
r"did", # not adding DID here as the output maycome with did or DID but i need to check only when its all caps
searchstring,
)
uj5u.com熱心網友回復:
您可以通過以下方式找到所有大寫字母單詞:
import re
txt = "who did DID what Did DiD?"
result = re.findall(r"\b[A-Z] \b", txt)
print(result)
輸出:
['DID']
uj5u.com熱心網友回復:
我有一個字串 誰做了什么?只有當 DID 是大寫字母時,我才能在下面的正則運算式中添加什么來搜索?
output = re.findall( r"did", searchstring, )
如果要搜索大寫字母,只需搜索大寫字母:
output = re.findall(
r"DID",
searchstring,
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/432552.html
