我在类中有几种方法可以检查用户输入是否符合规则。 它们看起来像下面的例子:
def check1(self, value):
regex = re.compile(r'^(\w|/(?!/))+(-(\w|/(?!/))+)*$')
return re.search(regex, value)
或
def check2(self, value):
regex = re.compile("^(filter_|input_|output_|util_)\w+(-\w+)*$")
return re.search(regex, value
当re.search返回None时,我想捕获它失败的原因,这样我就可以向人类显示一条消息,引导他们纠正错误。
显示比以下更具体的内容会很好:
您只能使用字符A-Z,a-z,0-9或下划线,反斜杠 或连字符
如何最好地捕获已检查的值中的违规字符?
例如,我可以向用户显示:
不允许使用字符“@”
或
您必须使用其中一个单词('filter_','input_','output_'或'util _')启动数据
答案 0 :(得分:0)
与您现在使用的正则表达式匹配允许输入相反(或另外)捕获有问题的字符,您需要匹配禁用部分的正则表达式:
result = re.search(r'[^\w/-]', value)
if result:
print "The character \""+result.group(0)+"\" is not allowed."
result = re.search(r'^-', value)
if result:
print "A leading \"-\" is not allowed."
result = re.search(r'-$', value)
if result:
print "A trailing \"-\" is not allowed."
result = re.search(r'//', value)
if result:
print "Consecutive slashes are not allowed."
和
result = re.search(r'^(filter_|input_|output_|util_)', value)
if not result: # here you didn't say you want the offending characters
print "You must start the data with one of the words 'filter_', 'input_', 'output_' or 'util_'."
result = re.search(r'^(filter_|input_|output_|util_)(\W)', value)
if result:
print "A \""+result.group(2)+"\" is not allowed immediately following '"+result.group(1)+"'."
result = re.search(r'[^\w-]', value)
if result:
print "The character \""+result.group(0)+"\" is not allowed."
result = re.search(r'-$', value)
if result:
print "A trailing \"-\" is not allowed."