看板 Python 關於我們 聯絡資訊
請教一下各位大大 下面regular expression p = re.compile("([abc])+") text = "abc" match = p.match(text) print match.group(0) #印出來是abc print match.group(1) # 為什麼這裡印出來的是c啊,要怎麼看? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.220.35.20 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1466662203.A.862.html ※ 編輯: left (61.220.35.20), 06/23/2016 14:10:31
uranusjr: https://regex101.com/#python 06/23 14:18
uranusjr: 注意右上角解釋欄的 Note 06/23 14:19
left: A repeated capturing group will only capture 06/23 14:37
left: the last iteration ?? 06/23 14:38
left: 但後面的解釋看不太懂 06/23 14:39
left: Put a capturing group around the repeated 06/23 14:39
left: group to capture all iterations or use a non-capturing 06/23 14:40
left: group instead if you're not interested in the data 06/23 14:40
aweimeow: 因為你的括號是包住[abc] 而不是 [abc]+ 06/24 13:42
aweimeow: match.group(0) 我印象中會印出 text 的值 06/24 13:42
s860134: 他上面解釋就說 你括號中的 pattern 能匹配到多組時 06/25 00:37
s860134: 我們只會幫你抓最後一組呦~ 如果你想要抓整個重複的部分 06/25 00:39
s860134: 請你把加號包含到小括弧內,如果你對資料沒興趣,你可以 06/25 00:39
s860134: 不要用小括弧~ 06/25 00:39
s860134: (小括弧內的)資料 06/25 00:40