繁体   English   中英

在元组上使用python regex过滤用户输入

[英]using python regex on tuples to filter user inputs

我想使用正则表达式基于一组元组过滤用户输入。 如果在set of tuples找不到用户输入并且该输入不是an alphanumeric character则应返回错误消息。 我不知道如何在python regex代码中访问元组。 所以我传入了src.items() ,如何使用转义功能获取src.items()来引入其值,或者也许我不应该这样做。

我的代码:

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)

首先,这里有一个冗余:

如果在元组集中找不到用户输入并且该输入不是字母数字字符,则应该返回错误消息

如果用户输入位于您的元组集中,那么它如何包含非字母数字字符? 另外,您不指定是一次测试单个单词还是完整短语。

让我们尝试另一种方法。 首先,不要使用两个级别的数据结构(即只是字典)。其次,我们将元组切换为列表,不是出于技术原因,而是出于语义原因(同质->列表,异质->元组)。 现在,我们将使用正则表达式,以简单的split() in测试。 最后,我们将测试完整的短语:

vocabulary = {
    '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']
    }

vocabulary_list = [word for sublist in vocabulary.values() for word in sublist]

phrases = ["Go in the east door", "Stop at the cabinet", "Eat the bear", "Do my taxes"]

# use this to pick out error strings from user input
for phrase in phrases:
    if any(term.lower() not in vocabulary_list for term in phrase.split()):
        print phrase, "-> invalid"
    else:
        print phrase, "-> valid"

产品展示

Go in the east door -> valid
Stop at the cabinet -> valid
Eat the bear -> valid
Do my taxes -> invalid

从这里开始,您可能会考虑允许一些动词,例如逗号和句点,并简单地剥离它们而不是对其进行判断。

这不是使用正则表达式的好地方,这与有效的Python正则表达式完全不同。

最好只是在循环中检查用户输入(可能被强制为小写)是否等于任何命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM