[問題] regular expression

看板Python作者 (881 forever)時間9年前 (2016/06/23 14:10), 9年前編輯推噓1(1014)
留言15則, 4人參與, 最新討論串1/1
請教一下各位大大 下面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

06/23 14:18, , 1F

06/23 14:19, , 2F
注意右上角解釋欄的 Note
06/23 14:19, 2F

06/23 14:37, , 3F
A repeated capturing group will only capture
06/23 14:37, 3F

06/23 14:38, , 4F
the last iteration ??
06/23 14:38, 4F

06/23 14:39, , 5F
但後面的解釋看不太懂
06/23 14:39, 5F

06/23 14:39, , 6F
Put a capturing group around the repeated
06/23 14:39, 6F

06/23 14:40, , 7F
group to capture all iterations or use a non-capturing
06/23 14:40, 7F

06/23 14:40, , 8F
group instead if you're not interested in the data
06/23 14:40, 8F

06/24 13:42, , 9F
因為你的括號是包住[abc] 而不是 [abc]+
06/24 13:42, 9F

06/24 13:42, , 10F
match.group(0) 我印象中會印出 text 的值
06/24 13:42, 10F

06/25 00:37, , 11F
他上面解釋就說 你括號中的 pattern 能匹配到多組時
06/25 00:37, 11F

06/25 00:39, , 12F
我們只會幫你抓最後一組呦~ 如果你想要抓整個重複的部分
06/25 00:39, 12F

06/25 00:39, , 13F
請你把加號包含到小括弧內,如果你對資料沒興趣,你可以
06/25 00:39, 13F

06/25 00:39, , 14F
不要用小括弧~
06/25 00:39, 14F

06/25 00:40, , 15F
(小括弧內的)資料
06/25 00:40, 15F
文章代碼(AID): #1NQtqxXY (Python)
文章代碼(AID): #1NQtqxXY (Python)