新聞中心
Python正則表達(dá)式有很多的時(shí)候需要我們進(jìn)行子串的匹配,不單在進(jìn)行替換的時(shí)候需要進(jìn)行,在很多的地方都會(huì)用到這些代碼。下面我們就來(lái)詳細(xì)的學(xué)習(xí)如何用Python正則表達(dá)式獲取自己想得到的匹配子串。

創(chuàng)新互聯(lián)專注于瓜州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供瓜州營(yíng)銷型網(wǎng)站建設(shè),瓜州網(wǎng)站制作、瓜州網(wǎng)頁(yè)設(shè)計(jì)、瓜州網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造瓜州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供瓜州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
獲取Python正則表達(dá)式所匹配的子串(Get the part of a string matched by the regex)
- regex=ur"..." #正則表達(dá)式
- match = re.search(regex, subject)
- if match:
- result = match.group()
- else:
- result = ""
獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group)
- regex=ur"..." #正則表達(dá)式
- match = re.search(regex, subject)
- if match:
- result = match.group(1)
- else:
- result = ""
獲取有名組所匹配的子串(Get the part of a string matched by a named group)
- regex=ur"..." #正則表達(dá)式
- match = re.search(regex, subject)
- if match:
- result = match.group("groupname")
- else:
- result = ""
將字符串中所有匹配的子串放入數(shù)組中(Get an array of all regex matches in a string)
- reresult = re.findall(regex, subject)
遍歷所有匹配的子串(Iterate over all matches in a string)
- for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group()
通過Python正則表達(dá)式 字符串創(chuàng)建一個(gè)正則表達(dá)式對(duì)象(Create an object to use the same regex for many operations)
- rereobj = re.compile(regex)
用法1的Python正則表達(dá)式對(duì)象版本(use regex object for if/else branch whether (part of) a string can be matched)
- rereobj = re.compile(regex)
- if reobj.search(subject):
- do_something()
- else:
- do_anotherthing()
用法2的Python正則表達(dá)式對(duì)象版本(use regex object for if/else branch whether a string can be matched entirely)
- rereobj = re.compile(r"\Z")?。U齽t表達(dá)式末尾以\Z 結(jié)束
- if reobj.match(subject):
- do_something()
- else:
- do_anotherthing()
以上就是對(duì)Python正則表達(dá)式在獲取匹配子串應(yīng)用的介紹。
本文題目:Python正則表達(dá)式如何匹配子串
地址分享:http://m.fisionsoft.com.cn/article/djccpde.html


咨詢
建站咨詢
