您的位置:首页 > 编程语言 > Python开发

python实践项目(六)

2017-11-07 11:25 423 查看


练习1:强口令检测

          写一个函数,它使用正则表达式,确保传入的口令字符串是强口令。强口令的定义是:长度不少于8 个字符, 同时包含大写和小写字符, 至少有一位数字。你可能需要用多个正则表达式来测试该字符串,
以保证它的强度。

detectStrongPassword.py

#-*-coding:utf-8-*-

import pyperclip,re

def detection(text):
if(len(text)<8):
return False
number1 = re.compile(r'\d+')
if number1.search(text) == None:
return False
number2 = re.compile(r'[A-Z]+')
if number2.search(text) == None:
return False
number3= re.compile(r'[a-z]+')
if number3.search(text) == None:
return False
return True

print('Get the password that you want to set:')
# text = str(pyperclip.paste())      #从剪切板复制口令
text = input()                       #从命令行窗口输入口令
if detection(text):
print('The password is the strong password.')
else:
print('Warnning:the password is not  the strong password!')

练习2:strip()的正则表达式版本
写一个函数,它接受一个字符串,做的事情和 strip()字符串方法一样。如果只传入了要去除的字符串, 没有其他参数, 那么就从该字符串首尾去除空白字符。否则, 函数第二个参数指定的字符将从该字符串中去除。 

       (注:strip()字符串方法将返回一个新的字符串, 它的开头或末尾都没有空白字符。lstrip()和 rstrip()方法将相应删除左边或右边的空白符。)

#-*-coding:utf-8-*-

# 参考链接:http://blog.csdn.net/q547550831/article/details/70553223
import re

def strip(text, chars=None):
"""去除首尾的字符
:type text: string
:type chars: string
:rtype: string
"""
if chars is None:
reg = re.compile('^ *| *$')
else:
reg = re.compile(r'^[' + chars + ']*|[' + chars + ']*$')
return reg.sub('', text)

print(strip('   123456   '))                # 123456
print(strip('   123456'))                   # 123456
print(strip('   123456    '))               # 123456
print(strip('123456   654321'))             # 123456   654321
print(strip('123456   654321', '1'))        # 23456   65432
print(strip('123456   654321', '1234'))     # 56   65
print(strip('123456   654321', '124'))      # 3456   6543
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python实践项目