作者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/cn.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