一.正则表达式

1.特殊字符

1.非贪婪模式:

import re

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

2.[]的三种用法

1)[abcd]ooby123--任何一个

2)[0-9]--区间

3)[^1]--非

中括号里面的字符不再有特殊意义

import re

line="18sssssss22"
regex_str="(1[48357][^1]{9})"
match_obj=re.match(regex_str,line)
if match_obj:
    print(match_obj.group(1))

3.s S w W

s---空格(单个字符)

S----非空格(单个字符)

w---[a-zA-Z0-9 _]

 W---非w即可

4.实例

import re

#line="XXX出生于2001年6月1日"
# 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/chenshaoping/p/8987988.html