python怎麼根據Regex提取指定的內容

來源:互聯網
上載者:User

python怎麼根據Regex提取指定的內容:

Regex是極其強大的,利用Regex來提取想要的內容是很方便的事。

下面示範了在python裡,通過Regex來提取符合要求的內容。

執行個體代碼:

import re # Regex是極其強大的,利用Regex來提取想要的內容是很方便的事。 # 下面示範了在python裡,通過Regex來提取符合要求的內容。有幾個要注意 的地方就是: # [1] 要用()將需要的內容包含起來 # [2] 編號為0的group是整個符合Regex的內容,編號為1的是第一個(及對應的)包含的內容 # @param regex: regular expression, use () to group the result # Regex,用()將要提取的內容包含起來 # @param content:  # @param index: start from 1, depends on the \p regex's () #   從1開始,可以通過數(來得到,其中0是全部匹配 # @return: the first match of the \p regex #   只返回第一次匹配的內容 def extractData(regex, content, index=1):   r = '0'   p = re.compile(regex)   m = p.search(content)   if m:     r = m.group(index)   return r regex = r'第(.*)場雪' content = '2002年的第一場雪' index = 1 print extractData(regex, content, index) 

以上便是怎麼使用python根據Regex提取指定的內容的全部介紹了,希望大家在看完之後都能有所收穫。

聯繫我們

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