正则表达式

2020-03-25  本文已影响0人  loonytes

特殊字符:
(1)^$*?+{2}{2,}{2,5}
(2)[][^][a-z]
(3)\s \S \w \W
(4)[\u4E00-\u9FA5] () \d

总结表
import re

line="booby123"
regex_str="^b.*"
if re.match(regex_str,line):
    print("yes")
import re

line="booby123"
regex_str="^b.*"
if re.match(regex_str,line):
    print("yes")

匹配中的贪婪模式

import re

line="boooooobby123"
regex_str=".*(b.*b).*"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))

输出结果为bb

原因是正则表达式的贪婪匹配模式。

贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。

非贪婪匹配:就是匹配到结果就好,就少的匹配字符。

import re

line="boooooobby123"
regex_str=".*?(b.*b).*?"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))

默认是贪婪模式;在量词后面直接加上一个问号?就是非贪婪模式。在本例中就是在*后加上?

没有+的情况:

import re

line="boooooobnnvccbby123"
regex_str=".*(b.*b).*"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    
结果:bb

有+的情况

import re

line="boooooobnnvccbby123"
regex_str=".*(b.+b).*"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    
结果:bnnvccbb

即使是贪婪匹配方式,也不会输出bb

import re

line="boooooobnnvccbaby123"
regex_str=".*(b.{1}b).*"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    
结果:bab
import re

line="boooooobnnvccbaaaby123"
regex_str=".*(b.{1,3}b).*"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    
结果:baaab
import re

line="booby123"
regex_str="(booby|booby123)"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
import re

line="booby123"
regex_str="(booby|boooby)123"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
   
//局部匹配 前面满足booby或boooby均可以
import re

line="booby123"
regex_str="([abcd]ooby123)"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
import re

line="15764289384"
regex_str="(1[3578][0-9]{9})"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    
字符串要求:第一位数字为1,第二位数字为3 5 7 8 中的任意一个,之后为9个0-9之间的字符
    
    (^表示非 只要非1就可以 即如果未15SSSSSSSSS也符合要求)
import re

line="你 好"
regex_str="(你\s好)"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    
输出 你好
import re

line="你很不好"
regex_str="(你\S+好)"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
import re

line="你S好"
regex_str="(你\w好)"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
import re

line="你 好"
regex_str="([\u4E00-\u9FA5]+)"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    
结果为 你

使用该替代提取大学名称

import re

line="stydy in 南京大学"
regex_str=".*?([\u4E00-\u9FA5]+大学)"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))

结果为 南京大学
import re

line="XXX出生于2001年"
regex_str=".*?(\d+)年"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))

正则表达式小练习:匹配不同的出生日期写法

import re

#line="XXX出生于2001年6月1日"
#line="XXX出生于2001/6/1"
#line="XXX出生于2001-06-01"
line="XXX出生于2001-06"
regex_str= '.*?出生于(\d{4}[年/-]\d{1,2}([月/-]\d{1,2}|[月/-]$|$))'
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))
    

https://www.cnblogs.com/chengege/p/11190782.html

上一篇 下一篇

猜你喜欢

热点阅读