新聞中心
我們?cè)谟?jì)算機(jī)應(yīng)用方面經(jīng)常會(huì)遇到很多的困難,例如下面列出Python正則表達(dá)式,就Python正則表達(dá)式中經(jīng)常出現(xiàn)的問(wèn)題我們給出以下的幾種匹配用法,希望大家在這篇文章中對(duì)Python正則表達(dá)式有一個(gè)更好的了解。

創(chuàng)新互聯(lián)建站主營(yíng)昆山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā),昆山h5微信小程序開(kāi)發(fā)搭建,昆山網(wǎng)站營(yíng)銷推廣歡迎昆山等地區(qū)企業(yè)咨詢
1.測(cè)試正則表達(dá)式是否匹配字符串的全部或部分
- regex=ur"" #正則表達(dá)式
- if re.search(regex, subject):
- do_something()
- else:
- do_anotherthing()
2.測(cè)試正則表達(dá)式是否匹配整個(gè)字符串
- regex=ur"\Z" #正則表達(dá)式末尾以\Z結(jié)束
- if re.match(regex, subject):
- do_something()
- else:
- do_anotherthing()
3.創(chuàng)建一個(gè)匹配對(duì)象,然后通過(guò)該對(duì)象獲得匹配細(xì)節(jié)(Create an object with details about how the regex matches (part of) a string)
- regex=ur"" #正則表達(dá)式
- match = re.search(regex, subject)
- if match:
- # match start: match.start()
- # match end (exclusive): atch.end()
- # matched text: match.group()
- do_something()
- else:
- do_anotherthing()
4.獲取正則表達(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 = ""
5. 獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group)
- regex=ur"" #正則表達(dá)式
- match = re.search(regex, subject)
- if match:
- result = match.group"groupname")
- else:
- result = ""
6. 獲取有名組所匹配的子串(Get the part of a string matched by a named group)
新聞名稱:Python正則表達(dá)式的幾種匹配方法
文章轉(zhuǎn)載:http://www.dlmjj.cn/article/dpcpcop.html


咨詢
建站咨詢
