How to capture multiple repeated groups?

前端 未结 7 1523
傲寒
傲寒 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:16

    Just to provide additional example of paragraph 2 in the answer. I'm not sure how critical it is for you to get three groups in one match rather than three matches using one group. E.g., in groovy:

    def subject = "HELLO,THERE,WORLD"
    def pat = "([A-Z]+)"
    def m = (subject =~ pat)
    m.eachWithIndex{ g,i ->
      println "Match #$i: ${g[1]}"
    }
    
    Match #0: HELLO
    Match #1: THERE
    Match #2: WORLD
    

提交回复
热议问题