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

【20180221】python-tips-正则匹配

2018-02-21 22:58 260 查看
python code:
import re
rgx_digit=re.compile(r'.*[0-9].*')
rgx_upcha=re.compile(r'.*[A-Z].*')
rgx_lowcha=re.compile(r'.*[a-z].*')
password=raw_input("pls input password")
print("password is %s " % password)

if rgx_digit.match(password) is not None and rgx_lowcha.match(password) is not None and rgx_upcha.match(password) is not None:
print("password is ok")
else:
print("password is wrong")

result=re.search('[0-9]|[a-z]|[A-Z]',password)
new_result=re.search(r'((?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).*)',password)

print(result)
print(new_result)reference:http://f.dataguru.cn/thread-107825-1-1.html http://bbs.csdn.net/topics/390051144
正则表达式-问号的四种用法     
原文符号

因为?在正则表达式中有特殊的含义,所以如果想匹配?本身,则需要转义,\?

有无量词

问号可以表示重复前面内容的0次或一次,也就要么不出现,要么出现一次。

非贪婪匹配

贪婪匹配
在满足匹配时,匹配尽可能长的字符串,默认情况下,采用贪婪匹配
stringpattern1 = @"a.*c"; // greedy match 
Regex regex = newRegex(pattern1);
regex.Match("abcabc"

; // return "abcabc"

非贪婪匹配
在满足匹配时,匹配尽可能短的字符串,使用?来表示非贪婪匹配
stringpattern1 = @"a.*?c"; // non-greedy match 
Regex regex = newRegex(pattern1); 
regex.Match("abcabc"); // return "abc"

几个常用的非贪婪匹配Pattern
*? 重复任意次,但尽可能少重复
+? 重复1次或更多次,但尽可能少重复
4000

?? 重复0次或1次,但尽可能少重复
{n,m}? 重复n到m次,但尽可能少重复

{n,}? 重复n次以上,但尽可能少重复
预见匹配匹配到括号中的字符串时,并不将匹配的指针后移,所以,它可以同时在一行中匹配多个字符串,并且不用考虑先后顺序。

肯定的和否定的预见匹配 
肯定的预见匹配语法为/pattern(?=string)/,其意义为匹配后面为string的模式,相反的,(?!string)意义为匹配后面非string的模式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: