How to capture multiple repeated groups?

前端 未结 7 1594
傲寒
傲寒 2020-11-22 10:59

I need to capture multiple groups of the same pattern. Suppose, I have a following string:

HELLO,THERE,WORLD

And I\'ve written a following

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 11:18

    With one group in the pattern, you can only get one exact result in that group. If your capture group gets repeated by the pattern (you used the + quantifier on the surrounding non-capturing group), only the last value that matches it gets stored.

    You have to use your language's regex implementation functions to find all matches of a pattern, then you would have to remove the anchors and the quantifier of the non-capturing group (and you could omit the non-capturing group itself as well).

    Alternatively, expand your regex and let the pattern contain one capturing group per group you want to get in the result:

    ^([A-Z]+),([A-Z]+),([A-Z]+)$
    

提交回复
热议问题