作者left (881 forever)
看板Python
標題[問題] regular expression
時間Thu Jun 23 14:09:59 2016
請教一下各位大大
下面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://webptt.com/m.aspx?n=bbs/Python/M.1466662203.A.862.html
※ 編輯: left (61.220.35.20), 06/23/2016 14:10:31
2F:→ uranusjr: 注意右上角解釋欄的 Note 06/23 14:19
3F:→ left: A repeated capturing group will only capture 06/23 14:37
4F:→ left: the last iteration ?? 06/23 14:38
5F:→ left: 但後面的解釋看不太懂 06/23 14:39
6F:→ left: Put a capturing group around the repeated 06/23 14:39
7F:→ left: group to capture all iterations or use a non-capturing 06/23 14:40
8F:→ left: group instead if you're not interested in the data 06/23 14:40
9F:推 aweimeow: 因為你的括號是包住[abc] 而不是 [abc]+ 06/24 13:42
10F:→ aweimeow: match.group(0) 我印象中會印出 text 的值 06/24 13:42
11F:→ s860134: 他上面解釋就說 你括號中的 pattern 能匹配到多組時 06/25 00:37
12F:→ s860134: 我們只會幫你抓最後一組呦~ 如果你想要抓整個重複的部分 06/25 00:39
13F:→ s860134: 請你把加號包含到小括弧內,如果你對資料沒興趣,你可以 06/25 00:39
14F:→ s860134: 不要用小括弧~ 06/25 00:39
15F:→ s860134: (小括弧內的)資料 06/25 00:40