Python programming quick start Chapter 7 practical project exercises

1. Strong password detection:
Write a function that uses regular expressions to determine that the incoming password string is a strong password. The definition of a strong password is: the length is not less than 8 characters, contains both uppercase and lowercase characters, and has at least one digit. You may need multiple regular expressions to test the string for its strength.
code show as below:

class ValueErro(Exception) : pass

class checkPWD:
    '''
    密码条件:必须要有大写字母、小写字母、数字,中间不能有空格。
    '''
    def __init__(self,cstr) :
        self.cstr = cstr

    def docheck(self,cstr):
        cstr = cstr.strip()
        cReturn = True
        
        if len(self.cstr) < 8 :
            raise ValueError('密码长度不足8位。')
            cReturn = False
        
        if cstr.find(' ') > 0 :
            raise ValueError('密码中不能有空格。')
            cReturn = False

        import re

        #查找数字
        __num = re.compile(r'\d?')  #有问号
        g = __num.findall(cstr)
        print(g)
        if len(g) <= 0 :
            raise ValueError('密码中必须要有数字!')
            cReturn = False

        #查找大写字母
        __upp = re.compile(r'[ABCDEFGHIJKLMNOPQRSTUVWXYZ]?')  #有问号
        g = __upp.findall(cstr)
        print(g)
        if len(g) <= 0 :
            raise ValueError('密码中必须要有大写字母!')
            cReturn = False

        #查找小写字母
        __low = re.compile(r'[abcdefghijklmnopqrstuvwxyz]?')  #有问号
        g = __low.findall(cstr)
        print(g)
        if len(g) <= 0 :
            raise ValueError('密码中必须要有小写字母!')
            cReturn = False

        return cReturn

pwd = 'abcde888Df'
p = checkPWD(pwd)   # 如果不带pwd,会报错:需要cstr参数
print(p.docheck(pwd)) #输出:True

Output:
['', '', '', '', '', '8', '8', '8', '', '', ''] ['', '', '',
' ', '', '', '', '', 'D', '', '']
['a', 'b', 'c', 'd', 'e', ​​'', '' , '', '', 'f', '']
True

Note the difference between the next piece of code and the previous piece of code:

class ValueErro(Exception) : pass

class checkPWD:
    '''
    密码条件:必须要有大写字母、小写字母、数字,中间不能有空格。
    '''
    def __init__(self,cstr) :
        self.cstr = cstr

    def docheck(self,cstr):
        cstr = cstr.strip()
        cReturn = True
        
        if len(self.cstr) < 8 :
            raise ValueError('密码长度不足8位。')
            cReturn = False
        
        if cstr.find(' ') > 0 :
            raise ValueError('密码中不能有空格。')
            cReturn = False

        import re

        #查找数字
        __num = re.compile(r'\d')  #不带问号
        g = __num.findall(cstr)
        print(g)
        if len(g) <= 0 :
            raise ValueError('密码中必须要有数字!')
            cReturn = False

        #查找大写字母
        __upp = re.compile(r'[ABCDEFGHIJKLMNOPQRSTUVWXYZ]')  #不带问号
        g = __upp.findall(cstr)
        print(g)
        if len(g) <= 0 :
            raise ValueError('密码中必须要有大写字母!')
            cReturn = False

        #查找小写字母
        __low = re.compile(r'[abcdefghijklmnopqrstuvwxyz]')  #不带问号
        g = __low.findall(cstr)
        print(g)
        if len(g) <= 0 :
            raise ValueError('密码中必须要有小写字母!')
            cReturn = False

        return cReturn

pwd = 'abcde888Df'
p = checkPWD(pwd)   # 如果不带pwd,会报错:需要cstr参数
print(p.docheck(pwd)) #输出:True

Output:
['8', '8', '8']
['D']
['a', 'b', 'c', 'd', 'e', ​​'f']
True

Second, the regular expression version of strip()
Write a function that accepts a string and does the same thing as the strip() string method. If only the string to be stripped is passed in without other parameters, whitespace characters are stripped from the beginning and end of the string. Otherwise, the characters specified by the function's second parameter are stripped from the string.
code show as below:

	def test(cstr,cword):
    import re

    while True :
        mo = re.search('^ ', cstr)  #从头找空格
        if mo != None :  #如果mo为None,则表示没有匹配的字符串
            p = mo.group()
            if len(p) > 0 :
                cstr = cstr[1:]  #从第2位向右截取字符串
                print(cstr)
                continue
        else :
            break

    while True :
        mo = re.search(' $', cstr)  #从尾找空格
        if mo != None :  #如果mo为None,则表示没有匹配的字符串
            p = mo.group()
            if len(p) > 0 :
                cstr = cstr[:len(cstr)-1]
                print(cstr)
                continue
        else :
            break

    if cword != None :
        while True :
            cplace = cstr.find(cword)
            if cplace == -1 :
                break
            else :
                cstr = cstr[:cplace] + cstr[cplace+1:]
                continue
    return cstr

print(test('   abc-123   ','-'))

Output:
abc123

Guess you like

Origin blog.csdn.net/any1where/article/details/128154698