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 项目实践三(Web应用程序)第四篇
- Python 实践项目 游戏
- python实践项目(二)
- Python 项目实践三(Web应用程序)第四篇
- python开源项目目录结构参考和django最佳实践:项目布局
- Python 项目实践三(Web应用程序)第五篇
- python编程快速上手之第10章实践项目参考答案
- 实践项目四:桌面版python 查询天气(3)
- Python & 机器学习之项目实践
- Python 项目实践一(外星人入侵小游戏)第五篇
- Python 项目实践三(Web应用程序)第五篇
- python编程快速上手之第5章实践项目
- Python编程快速上手 让繁琐工作自动化 | 第三章 :实践项目
- python实践项目(四)
- 实践项目五:python调用百度API实现自动检测翻译
- Python 项目实践一(外星人入侵小游戏)第五篇
- python计算机视觉项目实践
- 实践项目七:模拟银行转账系统(python+mysql)
- python编程快速上手之第6章实践项目
- 转载-Python & 机器学习之项目实践