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

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


咨詢
建站咨詢
