为什么在尝试调用re.sub时我的代码抛出属性错误?

时间:2018-12-10 14:24:33

标签: python python-3.x

我有以下代码,可以使用x模块检查字符串re是否具有大写,小写和数字。为什么第9行抛出

AttributeError: '<invalid type>' object has no attribute 'sub' on line 9 in main.py

难道不就叫re.sub吗?

import re as regex

x = input()
y = 0
uppercase = 0
lowercase = 0
num = 0

def next():
    if len(regex.sub(r'[a-z]', '', x)) != len(x):
        lowercase = 1
    if len(regex.sub(r'[A-Z]', '', x)) != len(x):
        uppercase = 1
    if len(regex.sub(r'[1-9]', '', x)) != len(x):
        num = 1

if len(x) >= 6:
    if len(x) <= 12:
        next()
    else:
        print("NOPE, between 6,12 chars")
else:
    print("NOPE, between 6,12 chars")

1 个答案:

答案 0 :(得分:0)

错误的原因是因为我使用的在线编译器有一个名为re.py的文件