Python3 Regex,python3Regex

來源:互聯網
上載者:User

Python3 Regex,python3Regex

  1 '''  2 Regex,又稱規則運算式,在代碼中常簡寫為regex、regexp或RE,是電腦科學的一個概念。  3 正則表通常被用來檢索、替換那些符合某個模式(規則)的文本。  4 Regex是對字串(包括一般字元(例如,a 到 z 之間的字母)和特殊字元(稱為“元字元”))操作的一種邏輯公式,  5 就是用事先定義好的一些特定字元、及這些特定字元的組合,組成一個“規則字串”,這個“規則字串”用來表達對字串的一種過濾邏輯。  6 Regex是一種文字模式,模式描述在搜尋文本時要匹配的一個或多個字串。  7 '''  8   9 ''' 10 首先要清楚,字串提供的方法是完全符合 11 Regex則會給我們提供模糊比對,通過re模組調用 12 ''' 13 # a = 'Hobbyer is a student!' 14 # print(a.find('bby'))        # 尋找 15 # chg = a.replace('is','are')     # 替換元素 16 # print(chg) 17 # print(a.split('u'))     # 分割 18 ''' 19 部落格園 Infi_chu 20 ''' 21  22 import re       # 調用re模組 23 # print(re.findall('b\b{2}l','Hobbyer is a student!'))      # findall(規則,字串,修改匹配規則),完全比配 24  25 ''' 26 正則元字元(11個): 27 .   ^   $   *   +   ?   {}   []   |   ()   \ 28 ''' 29 # #  . 是萬用字元 30 # a1 = 'hello world' 31 # print(re.findall('w..l',a1))    # . 只能匹配任意一位,任意一個字元,多了就會不識別 32 # print(re.findall('w..l','w\norld'))     # . 不能匹配分行符號,除了分行符號都可以匹配一位 33 # print(re.split('[a,c]','ddabcffadacd'))     # 有順序的分割,中間同時出現ac所以會出現空 34 # print(re.sub('e.l','wc',a1))        # 替換 35 # 36 # a3 = re.compile('\.com')    # 多次用的方法,提高效率 37 # a3_out = a3.findall('www.example.com.cn') 38 # print(a3_out) 39 ''' 40 部落格園 Infi_chu 41 ''' 42 # #  ^ 43 # print(re.findall('w...d',a1)) 44 # print(re.findall('^w...d',a1))      # 從最開始匹配,如果有則出現,沒有則匹配不到 45 # 46 # #  $ 47 # print(re.findall('h...o$',a1))      # 只在結尾匹配 48 # 49 # #  * 重複前面字元,大於等於零次 50 # print(re.findall('b.*u','http://www.baidu.com'))    # 重複匹配,好幾個點可用一個*表示 51 # print(re.findall('.*',a))       # 輸出結果後方的'',是另外的情況(空情況,匹配0) 52 ''' 53 部落格園 Infi_chu 54 ''' 55 # #  + 重複匹配,大於等於1次 56 # print(re.findall('.+',a))       # 輸出結果必須有,則不會出現空的情況 57 # 58 # #  ?  匹配範圍零次或1次 59 # print(re.findall('w?r','wwrr'))     # 可以匹配0-1個字元 60 # 61 # #  {} 匹配任意次數 62 # print(re.findall('w{5}r','wwwrrwwwwwrr'))       # 匹配5個w和1個r 63 # print(re.findall('w{1,5}r','wwwrrwwwwwrrwr'))    # 匹配1-5次 64 ''' 65 部落格園 Infi_chu 66 ''' 67 # #  []  是字元集 68 # print(re.findall('w[c,e]r','wer'))      # []中可以添加任一字元或字串,多選1 69 # print(re.findall('w[a-z]','wff'))       # 範圍a-z 70 # print(re.findall('[a,*]','ww'))          # * 不在是之前的功能,在[]之中只是普通的*號,但是(\ ^ -)例外 71 # print(re.findall('[a-z,0-9,A-Z]','afsasdSFA54asS')) 72 # print(re.findall('[^c]','acs'))         # ^ 在[]是取反的意思 73 # print(re.findall('[^a,b]','abcdefg'))  # 非a非b 74 ''' 75 部落格園 Infi_chu 76 ''' 77 # \ 78 ''' 79 反斜線後邊跟元字元,使其去除特殊功能, 80 反斜線後邊跟一般字元,使其具有特殊功能 81 \d  匹配十進位數 82 \D  匹配任何非數字字元 83 \s  匹配任何空白字元 84 \S  匹配任何非空白字元 85 \w  匹配任何字母字元 86 \W  匹配任何非字母字元 87 \b  匹配一個單詞和空格間的位置 88 ''' 89 # print(re.findall('\d{2}','asdw5d31asdw1a3d5s48w4d3a1w')) # 匹配兩位元字 90 # print(re.findall('\swww','fg www')) # 匹配空白字元 91 # print(re.findall(r's\b','s is a s$udent'))  # \b匹配了特殊字元 92 # print(re.findall(r's\b','s is a student')) 93 # c1 = re.search('wc','wsdwcasdwcaff')      # search 只匹配找到的第一個 94 # print(c1) 95 # print(c1.group())       # 直接輸出匹配內容,但是search沒有匹配成功,調用group會報錯 96 # print(re.findall('\\\\c','afekK:\c'))      # 這裡注意Python解譯器中的轉義,交給re模組之後又有轉義,所以需要4個 97 # print(re.search(r'\bbasd','basd'))         # r表示原生字串,不需要轉義 98 ''' 99 部落格園 Infi_chu100 '''101 # #  ()  分組102 # print(re.search('(wc)+','fswfefwcdwc'))     # 分組103 # print(re.search('(wc)+','fswfefwcwc'))104 '''105 部落格園 Infi_chu106 '''107 # # |  或108 # print(re.search('(wc)|ff','ffwca'))     # 或109 110 '''111 較進階的用法112 '''113 # print(re.search('(?P<id>\d{3})/(?P<name>\w{3})','weeew34ttt123/ooo'))       # ?P<>起名的格式,名字放在<>中,後面為匹配規則114 # print(re.search('(?P<id>\d{3})/(?P<name>\w{3})','weeew34ttt123/ooo').group())115 # print(re.search('(?P<id>\d{3})/(?P<name>\w{3})','weeew34ttt123/ooo').group('id'))116 # print(re.search('(?P<id>\d{3})/(?P<name>\w{3})','weeew34ttt123/ooo').group('name'))117 118 '''119 方法總結120 Regex的方法:121 findall():返回所有的結果,返回到1個列表當中122 search():返回一個對象(object),返回匹配到的第一個對象,對象可以調用group()方法返回匹配內容123 match():只在字串開始的時候匹配,返回第一個匹配到的對象124 split():分割點125 sub():替換126 compile():可以用來規定規則127 '''128 129 130 '''131 部落格園 Infi_chu132 '''

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.