我想使用正則表達式來基於一組元組來過濾用戶輸入。如果在set of tuples
中找不到用戶輸入,並且不是an alphanumeric character
,則應該返回錯誤消息。我不知道如何訪問我的python正則表達式代碼中的元組。所以我通過了src.items()
,我該如何使用逃生功能讓src.items()
帶來它的價值,或者我不應該這樣做。在元組上使用python正則表達式來過濾用戶輸入
我的代碼:
import re
direction = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back')
verb = ('go', 'stop', 'kill', 'eat')
stop = ('the', 'in', 'of', 'from', 'at', 'it')
noun = ('door', 'bear', 'princess', 'cabinet')
src = {'direction': direction,
'verb': verb,
'stop': stop,
'noun': noun
}
# use this to pick out error strings from user input
er = r"*[\W | src.items()]"
ep = re.compile(er, re.IGNORECASE)
我一次測試完整的短語。 –