python 正则表达式 demo

1、匹配大小写和数字,并且大小写数字均要有,且字符串长度为6~20位

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,20}$

import re
pattern = re.compile("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,20}$")
flag = pattern.match("abcDEF123")
if flag:
print "String matches the pattern."
else:
print "String doesn't match the pattern."
(?=.*\d):表示字符串中有数字
(?=.*[a-z]):表示字符串中有小写字母
(?=.*[A-Z]):表示字符串中有大写字母
[a-zA-Z\d]{6,20}:表示字符串中大小写字符和数字总长为6~20之间
^$:分别表示字符串开头和结尾 http://hlee.iteye.com/blog/327993
上一篇:win10中android studio中的terminal不能输入


下一篇:No.025:Reverse Nodes in k-Group