正则表达式未正确计算最小值/最大值

时间:2019-01-21 21:47:04

标签: python regex validation

edit:我简化了正则表达式以获得正确的输出(请参见下面的答案)。我仍然希望就正则表达式为何无法正常工作提出建议。

我正在尝试逐行验证.csv文件中的数据。如果它匹配给定的参数,那就太好了。如果不是,那就不好。有问题的行是IWpfTextViewMargin行。第一个username会进行验证,而第二个if则不会(这是故意的)。

我认为elif的值仅应在if语句的范围内有效,但是我也尝试在运行matchObj之前设置matchObj = None,但仍然没有产生正确的输出。

我已附上我的完整代码以供参考。我正在用Python3编写代码。请问潜在的愚蠢问题,我来自Java / C。

.match

import re with open("input.csv", "r") as insert: array = [] for line in insert: array.append(line) for i in range(len(array)): tempList = array[i].split(',') print(tempList[0]) if tempList[0] == 'student': matchObj = re.match('\d{3}[\s]?\d{3}[\s]?\d{3}', tempList[1]) if matchObj: print('student = Right!') elif tempList[0] == 'password': matchObj = re.match('(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{12,}', tempList[1]) if matchObj: print('password = Right!') elif tempList[0] == 'username': matchObj = re.match('(?=.*[a-z])(?=.*[A-Z]).{3,20}', tempList[1]) print(matchObj) print(tempList[0]) print(tempList[1]) if matchObj: print('username = Right!') 的第三个elif应该返回“对!”。它可以包含任何A-Z字符,大小写无关或任何数字。应该在3到20个字符之间。我的.csv中的示例输入为username

这是.csv

user123

2 个答案:

答案 0 :(得分:1)

我简化了正则表达式以使其正常工作。

^[a-zA-Z0-9]{3,20}$

答案 1 :(得分:0)

让我们看看您的正则表达式本身:(?=.*[a-z])(?=.*[A-Z]).{3,20}

据我所知,这是不同小组的工作:

  • (?=.*[a-z])-对任意字符(换行符除外)进行多次重复的前瞻性断言,后跟一个小写字母。
  • (?=.*[A-Z])-对任意字符(换行符除外)进行多次重复的前瞻性断言,后跟一个大写字母。
  • .-接下来查找不是换行符的其他任何字符。
  • {3,20}-匹配前一个字符3到20次

例如,当我运行此代码时:

test_strings = [ 'Papa', 'papa', 'pA', 'pA!', 'pa&Pa', 'pApa', 'pa\nPa' ]
for s in test_strings:
    m = re.match('(?=.*[a-z])(?=.*[A-Z]).{3,20}', s)
    if m:
        print('"%s" is good' % s)
    else:
        print('"%s" is BAD' % s)

我得到这些结果:

"Papa" is good
"papa" is BAD
"pA" is BAD
"pA!" is good
"pa&Pa" is good
"pApa" is good
"pa
   Pa" is BAD

但是,如果您只想验证tempList[1]是仅由字母数字字符组成的字符串,那么作为答案使用的简化正则表达式将更有意义。实际上,由于您要匹配整个字符串,因此^$似乎是多余的。

对于它的价值,这是一种无需regexp就可以在Python中完成的方法:

matchObj = tempList[1].isalnum() and len(tempList[1]) in range(3,21)