python正则表达式示例

------(?iLmsux)---------re.i|re.L...---------------------------------------------------------------
>>> print re.findall(r'(?i)bd','abDcc134')
['bD']
>>> print re.findall(r'bd','abDcc134')
[]
>>> print re.findall(r'(?iLmsux)bd','abDcc134')
['bD']


------(?:...){m,n}---------...{m,n}合并未一组匹配--------------------------------------------------
>>> print re.findall(r'(?:abc)','abcabcabcabcabc')
['abc', 'abc', 'abc', 'abc', 'abc']
>>> print re.findall(r'(?:abc){3}','abcabcabcabcabc')
['abcabcabc']
>>> print re.findall(r'(?:abc){}','abcabcabcabcabc')
[]
>>> print re.findall(r'(?:abc)+','abcabcabcabcabc')
['abcabcabcabcabc']
>>> print re.findall(r'(?:abc)*','abcabcabcabcabc')
['abcabcabcabcabc', '']
>>> print re.findall(r'(?:abc)','abcabcabcabcabc')
['abc', 'abc', 'abc', 'abc', 'abc']


------(?#...)---------...注释-----------------------------------------------------------------------
>>> print re.findall(r'(?#abc)','abcabcabcabcabc')
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
>>> print re.findall(r'a(?#abc)bc','abcabcabcabcabc')
['abc', 'abc', 'abc', 'abc', 'abc']


------(?=...)---------...作为匹配条件(匹配字符后必须匹配...),但不计入匹配字符----------------------
>>> print re.findall(r'a(?=\d)','abcabcabcabcabca12')
['a']
>>> print re.findall(r'(a|b)(?=\d)','abcabcabcabcabca12b12')
['a', 'b']


------(?!...)---------...作为反匹配条件(匹配字符后必须不匹配...),但不计入匹配字符------------------
>>> print re.findall(r'(a|b)(?!\d)','abca12b12')
['a', 'b']


------(?<=...)---------...作为前匹配条件(匹配字符前必须匹配...),但不计入匹配字符-------------------
>>> print re.findall(r'(?<=\d)(a|b)','abca12b12')
['b']


------(?<!...)---------...作为前反匹配条件(匹配字符前必须不匹配...),但不计入匹配字符-----------------
>>> print re.findall(r'(?<!\d)(a|b)','abca12b12')
['a', 'b', 'a']


------...?---------匹配的贪婪与非贪婪,python默认未贪婪,即尽可能匹配多的字符------------------------
>>> print re.findall(r'ab*','abbbbbbbbc')
['abbbbbbbb']
>>> print re.findall(r'ab*?','abbbbbbbbc')
['a']
>>> print re.findall(r'ab+?','abbbbbbbbc')
['ab']

你可能感兴趣的:(python,正则表达式)