difference between two regular expressions: [abc]+ and ([abc])+

前端 未结 5 1523
-上瘾入骨i
-上瘾入骨i 2020-12-06 18:44
In [29]: re.findall(\"([abc])+\",\"abc\")
Out[29]: [\'c\']

In [30]: re.findall(\"[abc]+\",\"abc\")
Out[30]: [\'abc\']

Confused by the grouped one.

5条回答
  •  旧巷少年郎
    2020-12-06 19:15

    Here's the way I would think about it. ([abc])+ is attempting to repeat a captured group. When you use "+" after the capture group, it doesn't mean you are going to get two captured groups. What ends up happening, at least for Python's regex and most implementations, is that the "+" forces iteration until the capture group only contains the last match.

    If you want to capture a repeated expression, you need to reverse the ordering of "(...)" and "+", e.g. instead of ([abc])+ use ([abc]+).

提交回复
热议问题