日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

re正则表达式

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 re正则表达式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 import re 2 import unicodedata 3 4 s = "a00xoghasalexjkdfldhfjk" 5 v = s.find("alex") 6 print(v) 7 8 k = "23412342353464565346" 9 # 正則模糊匹配 10 11 # print(re.findall("alex", s)) 12 # # .是通配符(除了換行之外) 13 # print(re.findall("a..x", s)) 14 # # ^ (開頭)和 $(結尾) 15 # print(re.findall("^a..x", s)) 16 # print(re.findall("h..k$", s)) 17 # # * (0-max) ;+(1,max) ; ? (0,1) 貪婪匹配 18 # b = "ccsrwrfdddddddddddkokosadkfod" 19 # print(re.findall("d*", b)) 20 # print(re.findall("d+", b)) 21 # print(re.findall("alex*", "asdhfale")) # 0-無窮大個 22 # print(re.findall("alex+", "asdhfale")) # 1-無窮大 23 # print(re.findall("alex?", "asdhfalex")) # 0-1個 24 # print(re.findall("alex?", "asdhfale")) # 0-1個 25 # 26 # # {}0-無窮大個 == * ; {1,} == + ; {6} 6次 27 # # {} 可以帶貪婪匹配 28 # print(re.findall("alex{1,4}", "asdhfalexxx")) # 1-4個 29 # print(re.findall("alex{6}", "asdhfalexxx")) # 必須是6個 30 # # ? 惰性匹配 31 # print(re.findall("alex*?", "asdhfalexxx")) 32 # print(re.findall("alex+?", "asdhfalexxx")) 33 # 34 # # []字符集 中沒有特殊符號(除了\-^) 35 # print(re.findall("www[oldboy baidu]", "wwwbaidu")) 36 # print(re.findall("x[ys]", "xyyszz")) 37 # print(re.findall("s[zby]a", "xyyszasya")) 38 # print(re.findall("s[zb,]a", "xyyszas,a")) 39 # 40 # print(re.findall("s[zb,]a", "xyyszas,a")) 41 # print(re.findall("s[zb*]a", "xyyscas,a")) 42 # 43 # # -之間 44 # print(re.findall("s[a-z]*", "sqaaaaerqwr")) 45 # print(re.findall("s[a-z]*", "sqaaaaerqwr9")) # [a-z] 之間 46 # 47 # print(re.findall("s[0-9]*", "s9qaaaaerqwr9")) # [a-z] 之間 48 # # ^非 49 # print(re.findall("s[^a-z]", "sqaaaaerqwr9")) # [a-z] 之間 50 # # \轉義 51 # print(re.findall("\([^()]*\)", "12*(34*6+2-5*(2-1))")) 52 # print(re.findall("\([^()]+\)", "12*(34*6+2-5*(2-1))")) 53 # 54 # # \d 【0-9】的數值 55 # print(re.findall("\d+", "12*(34*6+2-5*(2-1))")) # [0-9] 56 # print(re.findall("\D+", "12*(34*6+2-5*(2-1))")) # [^0-9] 57 # print(re.findall("\s+", "hello world"), "ssss") # \t\n\r\f\v 任何空白符 58 # print(re.findall("\S+", "hello world")) # [^\t\n\r\f\v] 59 # print(re.findall("\w+", "hello world")) # [0-9a-zA-Z_] 60 # print(re.findall("\W", "hello world")) # [^0-9a-zA-Z_] 61 # print(re.findall("\b", "hello world")) # 空格,&,# 62 # 63 # print(re.findall(r"I\b", "hello I am world")) # 64 # print(re.findall("I\\b", "hello I am world")) # 65 # 66 67 # print(re.findall("c\\\f", r"abcde\fgh")) 68 # # | 或 69 # print(re.findall("gh|f", "abcde|fgh")) 70 # # () 分組 71 # print(re.findall("(abf)*", r"abfabfabfh")) 72 # 73 # print(re.findall("(?P<name>\w+)", r"abfabfabfh")) 74 # 75 # # search 找到第一個就返回一個對象(需要用group取出),,findall找到所有滿足的結果放入列表 76 # 77 # print(re.search("\d+","23414afdfasf324fa")) 78 # print(re.search("\d+","23414afdfasf324fa").group()) 79 # # 分組命名 80 # print(re.search("(?P<name>[a-z]+)(?P<age>\d+)","23414alex324fa").group("name","age")) 81 # 82 # # match 成功返回對象,失敗啥也不返回 83 # print(re.match("\d+", "24dsd143f")) 84 # 85 # # split 分割 86 # print(re.split(" ", "hello abc asf")) 87 # print(re.split("[ |]", "hello abc|asf")) 88 # print(re.split("[ab]", "hebllo abc|asf")) 89 # # ["he","llo abc|asf"]->["he",“llo ","bc|asf"]-> 90 # # ["he","llo "," ","c|asf"]->["he","llo "," ","c|","sf"], 91 # print(re.split("[ab]", "abc")) 92 # # 替換 93 # print(re.sub("\d+","A","dsfaf123aasf42112dfa")) 94 # 95 # print(re.subn("\d+","A","dsfaf123aasf42112dfa")) 96 # 97 # # 規則,書寫規則 98 # com = re.compile("\d+") 99 # str1 = "qfaqs234rer1344" 100 # print(com.findall(str1)) 101 # 102 # com = re.compile("\d") 103 # print(com.findall(str1)) 104 # # 迭代器 105 # ite = com.finditer(str1) 106 # # next(ite) 107 # print(ite) 108 # # ?: 去除優先級 109 # print(re.findall("www\.(baidu|163)\.com","www.163.com")) # 163 110 # print(re.findall("www\.(?:baidu|163)\.com","www.163.com")) # www.163.com
# print(re.search("abc|bcd", "abc")) # search
# print(re.search("a(bc)|bcd", "abc").group())
# "\(9[^()]+\)"

print(re.findall("(abc)+", "abcabcabc")) # 給整體添加匹配
print(re.findall("abc+", "abcccabcabcfadfabc")) # ?:去除優先級,給c添加重復匹配 ?

?

轉載于:https://www.cnblogs.com/liuzhanghao/p/11064342.html

總結

以上是生活随笔為你收集整理的re正则表达式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。