幾行代碼入門正則表達式(python版)
__author__ ='***'
# re? reg正則
importre
line ="bobby123"
# 正則表達式貪婪匹配,從后往前
regex_str ="^b.*3$"# ^代表以什么開頭,.代表任意字符,*代表前邊字符可以重復任意多次,$以那個字符結尾的,量詞后面直接加上一個問號?變成非貪婪匹配
ifre.match(regex_str, line):
print("yes")
#非貪婪模式的理解,貪婪模式從后往前,加?變成非貪婪模式,則從前往后匹配
line2 ="bbooooooooooobby123"
regex_str2 =".*(b.*b).*"
match_obj2 = re.match(regex_str2, line2)
ifmatch_obj2:
print(match_obj2.group(1))# 提取出第一個括號里面匹配到的東西
line3 ="boooobaaaooobbbby123"
regex_str3 =".*?(b.*?b).*"
match_obj3 = re.match(regex_str3, line3)
ifmatch_obj3:
print(match_obj3.group(1))
line4 ="abbdabcd"
regex_str4 ="(a.*?d)"
match_obj4 = re.match(regex_str4,line4)
ifmatch_obj4:
print(match_obj4.group(1))
#+
line5 ="booooobaaaoooobbbbby123"
regex_str5 =".*(b.+b).*"
match_obj5 = re.match(regex_str5,line5)
ifmatch_obj5:
print(match_obj5.group(1))
#{}
line6 ="booooobaaaoooobbbaabby123"
regex_str6 =".*(b.{2}b).*"#{2,5}最少兩次,最多5次
match_obj6 = re.match(regex_str6,line6)
ifmatch_obj6:
print(match_obj6.group(1))
#|
line7 ="boobby123"
regex_str7 ="((bobby|boobby)123)"#{2,5}最少兩次,最多5次
match_obj7 = re.match(regex_str7,line7)
ifmatch_obj7:
print(match_obj7.group(2))
#[]滿足其中任意一個[0-9],,[^1]不是1
#\s:空格 \S:不是空格 \w==[A-Za-z0-9]
#漢字? [\u4E00-\u9FA5]+
line8 ="study in 清華大學"
regex_str8 =".*?([\u4E00-\u9FA5]+大學)"#{2,5}最少兩次,最多5次
match_obj8 = re.match(regex_str8,line8)
ifmatch_obj8:
print(match_obj8.group(1))
#\d
line9 ="xxx出生于2001年"
regex_str9 =".*?(\d+)年"#{2,5}最少兩次,最多5次
match_obj9 = re.match(regex_str9,line9)
ifmatch_obj9:
print(match_obj9.group(1))
#demo
#\d
line10 ="xxx出生于2001年6月1日"
line10 ="xxx出生于2001/6/1"
line10 ="xxx出生于2001-6-1"
line10 ="xxx出生于2001-06-01"
line10 ="xxx出生于2001-06"
regex_str10 =".*出生于(\d{4}[年/-]\d{1,2}([月/-]\d{1,2}|[月/-]$|$))"#{2,5}最少兩次,最多5次
match_obj10 = re.match(regex_str10,line10)
ifmatch_obj10:
print(match_obj10.group(1))