Python匹配如何才能完成匹配細(xì)節(jié)
Python匹配在我們使用的時(shí)候有很多的注意事項(xiàng)。我們?cè)诓粩嗟膶W(xué)習(xí)中會(huì)遇到不少的問(wèn)題。下面我們就詳細(xì)的看看如何才能更好的掌握相關(guān)的Python匹配技術(shù)問(wèn)題。用法2的正則表達(dá)式對(duì)象版本
- rereobj = re.compile(r"\Z")?。U齽t表達(dá)式末尾以\Z 結(jié)束
- if reobj.match(subject):
- do_something()
- else:
- do_anotherthing()
創(chuàng)建一個(gè)正則表達(dá)式對(duì)象,然后通過(guò)該對(duì)象獲得Python匹配細(xì)節(jié)
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group()
- do_something()
- else:
- do_anotherthing()
用正則表達(dá)式對(duì)象獲取Python匹配子串
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group()
- else:
- result = ""
用正則表達(dá)式對(duì)象獲取 捕獲組所Python匹配的子串
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group(1)
- else:
- result = ""
用正則表達(dá)式對(duì)象獲取 有名組所Python匹配的子串
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group("groupname")
- else:
- result = ""
用正則表達(dá)式 對(duì)象獲取所有Python匹配子串并放入數(shù)組
- rereobj = re.compile(regex)
- result = reobj.findall(subject)
通過(guò)正則表達(dá)式對(duì)象遍歷所Python有匹配子串
- rereobj = re.compile(regex)
- for match in reobj.finditer(subject):
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group
以上就是對(duì)Python匹配的相關(guān)細(xì)節(jié)介紹。
【編輯推薦】