日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python正則表達式如何匹配子串

Python正則表達式有很多的時候需要我們進行子串的匹配,不單在進行替換的時候需要進行,在很多的地方都會用到這些代碼。下面我們就來詳細的學(xué)習如何用Python正則表達式獲取自己想得到的匹配子串。

創(chuàng)新互聯(lián)專注于瓜州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供瓜州營銷型網(wǎng)站建設(shè),瓜州網(wǎng)站制作、瓜州網(wǎng)頁設(shè)計、瓜州網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造瓜州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供瓜州網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

獲取Python正則表達式所匹配的子串(Get the part of a string matched by the regex)

 
 
 
  1. regex=ur"..." #正則表達式
  2. match = re.search(regex, subject)
  3. if match:
  4. result = match.group()
  5. else:
  6. result = ""

獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group)

 
 
 
  1. regex=ur"..." #正則表達式
  2. match = re.search(regex, subject)
  3. if match:
  4. result = match.group(1)
  5. else:
  6. result = ""

獲取有名組所匹配的子串(Get the part of a string matched by a named group)

 
 
 
  1. regex=ur"..." #正則表達式
  2. match = re.search(regex, subject)
  3. if match:
  4. result = match.group("groupname")
  5. else:
  6. result = ""

將字符串中所有匹配的子串放入數(shù)組中(Get an array of all regex matches in a string)

 
 
 
  1. reresult = re.findall(regex, subject)

遍歷所有匹配的子串(Iterate over all matches in a string)

 
 
 
  1. for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
  2. # match start: match.start()
  3. # match end (exclusive): match.end()
  4. # matched text: match.group()

通過Python正則表達式 字符串創(chuàng)建一個正則表達式對象(Create an object to use the same regex for many operations)

 
 
 
  1. rereobj = re.compile(regex)

用法1的Python正則表達式對象版本(use regex object for if/else branch whether (part of) a string can be matched)

 
 
 
  1. rereobj = re.compile(regex)
  2. if reobj.search(subject):
  3. do_something()
  4. else:
  5. do_anotherthing()

用法2的Python正則表達式對象版本(use regex object for if/else branch whether a string can be matched entirely)

 
 
 
  1. rereobj = re.compile(r"\Z")?。U齽t表達式末尾以\Z 結(jié)束
  2. if reobj.match(subject):
  3. do_something()
  4. else:
  5. do_anotherthing()

以上就是對Python正則表達式在獲取匹配子串應(yīng)用的介紹。


網(wǎng)站名稱:Python正則表達式如何匹配子串
網(wǎng)站URL:http://www.5511xx.com/article/djccpde.html