Notes learning python "python programming quick start - let automate the tedious work of" Six

Exercise six
strong password detect
write a function that uses regular expressions to ensure that the string passed in password is a strong password. The definition of a strong password is: no less than eight characters in length, contain both uppercase and lowercase characters, at least one digit. You may need to test the string expressions with more positive, in order to ensure its strength.

#!python3
import re
def test_password(password):
	right=True	
	if len(password)>=8:
		rules=(r'\d+',r'[a-z]+',r'[A-Z]+')
		for rule in rules:
			ru=re.compile(rule)#初始正则表达式
			mo=ru.search(password)#匹配字符串
			try:
				mo.group()#一旦没有匹配会出现Attribte错误
			except AttributeError:
				right=False#如果出现错误证明缺少对应字符改变标志位
				num=rules.index(rule)#获取出现错误规则的下标
				if num==0:
					print('密码缺少数字')
				elif num==1:
					print('密码缺少小写字母')
				elif num==2:
					print('密码缺少大写字母')	
		if right:
			print('密码通过验证')
	else:
		print('密码少于8位')

strip () regular expression version
write a function that takes a string, do the same thing and strip () string method. If only the incoming string to be removed, no other parameters, then remove whitespace characters from the string end to end. Otherwise, the function of the second parameter specifies the character will be removed from the string.

def for_str(word,find=None):
	if find==None:#不传入find则按照空格处理
		l_strip=re.compile(r'^ +| +$')
		#r_strip=re.compile(r' +$')
		#x=l_strip.search(word).group()
		x=l_strip.sub('',word)
		print(x)
	else:#find不为空则替换匹配find值为空
		x_find=re.compile(find)
		result_f=x_find.sub('',word)
		print(result_f)
Published 23 original articles · won praise 5 · Views 388

Guess you like

Origin blog.csdn.net/weixin_43287121/article/details/104483626