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
posted @ 2015-01-06 14:02  泉水叮~咚  阅读(454)  评论(0编辑  收藏  举报