python正則學習一

來源:互聯網
上載者:User

標籤:dal   bdd   正則   findall   第一個   code   bss   add   字元集   

元字元之. ^ $ * + ? { }.匹配是:如果匹配一個就寫一個點,如果匹配多個字,就寫幾個點字>>> a = re.findall(‘b...dd‘,‘bsssdd‘)>>> print(a)[‘bsssdd‘]如:> a = re.findall(‘b..dd‘,‘bsssdd‘)>>> print(a)[]解釋:因為兩個點,需要匹配的中間有三個導致不能匹配^運算式:>>> re.findall(‘^a..s‘,‘abcsassss‘)[‘abcs‘]>>> re.findall(‘a..s‘,‘abcsassss‘)[‘abcs‘, ‘asss‘]>>> re.findall(‘^a..s‘,‘abcsassss‘)[‘abcs‘]>>>如果在逗號裡面支匹配一個,如果^在逗號外面是最大匹配內容。只有能滿足的都匹配出來。$匹配是從最後開始匹配,如果麼有提示空,如果有就匹配一個:>>> re.findall(‘a.b$‘,‘abbdddddabb‘)[‘abb‘]>>> re.findall(‘$a.b‘,‘abbdddddabb‘)[]*匹配:表示:如果匹配成功後,最後一個字元會最大去匹配內容:>>> re.findall(‘abc*‘,‘sabcccc‘)[‘abcccc‘]>>> re.findall(‘abc*‘,‘sabcccgewec‘)[‘abccc‘]>>>?運算式:如果匹配成功就停止匹配;>>> re.findall(‘abc?‘,‘abccc‘)[‘abc‘]>>> re.findall(‘abc+‘,‘abccc‘)[‘abccc‘]>>> re.findall(‘abc*‘,‘abccc‘)[‘abccc‘]>>> re.findall(‘abc?‘,‘abccc‘)[‘abc‘]{}匹配規則:現在條件如:{1,3}表示只有滿足1~3個中的任何一個就行。第一個數字需要小於第二個否則會出錯>>> re.findall(‘abc{1,4}‘,‘abccc‘)[‘abccc‘]>>>>>> re.findall(‘abc{1,4}‘,‘abcccccccccccccccc‘)[‘abcccc‘]>>> re.findall(‘abc{1,2}‘,‘abcccccccccccccccc‘)[‘abcc‘]元字元之字元集[]:表示裡面有幾個值是或者的關係,但只能是一個,如非加{才能限制長度匹配}>>> re.findall(‘a[b,c]{1}d?‘,‘abcddssaddddd‘)[‘ab‘]>>>

python正則學習一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.