Python匹配如何才能完成匹配細節
作者:佚名
Python匹配在使用的時候有不少的知識需要我們學習,下面我們就詳細的來了解下相關的知識。只有這樣在之后的使用中才能更順手。
Python匹配在我們使用的時候有很多的注意事項。我們在不斷的學習中會遇到不少的問題。下面我們就詳細的看看如何才能更好的掌握相關的Python匹配技術問題。用法2的正則表達式對象版本
- rereobj = re.compile(r"\Z") #正則表達式末尾以\Z 結束
- if reobj.match(subject):
- do_something()
- else:
- do_anotherthing()
創建一個正則表達式對象,然后通過該對象獲得Python匹配細節
- 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()
用正則表達式對象獲取Python匹配子串
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group()
- else:
- result = ""
用正則表達式對象獲取 捕獲組所Python匹配的子串
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group(1)
- else:
- result = ""
用正則表達式對象獲取 有名組所Python匹配的子串
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group("groupname")
- else:
- result = ""
用正則表達式 對象獲取所有Python匹配子串并放入數組
- rereobj = re.compile(regex)
- result = reobj.findall(subject)
通過正則表達式對象遍歷所Python有匹配子串
- rereobj = re.compile(regex)
- for match in reobj.finditer(subject):
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group
以上就是對Python匹配的相關細節介紹。
【編輯推薦】
責任編輯:張浩
來源:
CSDN