課程
/后端開發
/Python
/python正則表達式
ma = re.match(r'(hello)(world)?\2', 'helloworld')
2016-09-23
源自:python正則表達式 3-3
正在回答
匹配helloworld或者helloworldworld ? ?是這個意思嗎?
import?re ma?=?re.match(r'(hello)(world)\2?',?'helloworld') print?ma.group() ma?=?re.match(r'(hello)(world)\2?',?'helloworldworld') print?ma.group()
把?放在后面
風語者不語 提問者
因為你的正則表達式的前面一部分(hello)(world)?已經匹配了helloworld后面再加上\2肯定匹配不上的。
ma = re.match(r'(hello)(world)?\2', 'helloworldworld')這樣就可以匹配上了
舉報
如何使用正則處理文本,帶你對python正則有個全面了解
5 回答為什么只能匹配出第一個符合條件的字符串?
1 回答字符串匹配問題
1 回答匹配行結尾字符串
2 回答122行和123行字符串只匹配了第一個
1 回答正則可以匹配中間的字符串嗎
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-10-12
匹配helloworld或者helloworldworld ? ?是這個意思嗎?
把?放在后面
2016-09-23
因為你的正則表達式的前面一部分(hello)(world)?已經匹配了helloworld后面再加上\2肯定匹配不上的。
ma = re.match(r'(hello)(world)?\2', 'helloworldworld')這樣就可以匹配上了