【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"
![](http://f.dataguru.cn/static/image/smiley/qq/baiy.gif)
; // 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的模式
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"
![](http://f.dataguru.cn/static/image/smiley/qq/baiy.gif)
; // 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的模式
相关文章推荐
- Python tips: 什么是*args和**kwargs?
- Jython Tips for Python Programmers
- python tips
- [tips]PythonMagick的安装
- 交叉编译Python tips
- Python tips: 什么是*args和**kwargs?
- [Python Tips]去除 Trivial 赋值语句
- Python tips: 什么是*args和**kwargs
- Python-Tips
- PYTHON 容器TIPS
- Python tips: 什么是*args和**kwargs?
- Python Tips 01 : 判断两个文件是否相同
- python tips - 生成器
- Python学习中的tips(一)
- 学习Python的一些Tips
- Python tips
- python tricks/tips
- [tips]python图形处理库Python Imaging Library (PIL)简...
- python tips 系列 修改UNIX时区
- [Python Tips]去除 Trivial 赋值语句