How to capture multiple repeated groups?

前端 未结 7 1524
傲寒
傲寒 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
    
    0 讨论(0)
  • 2020-11-22 11:17

    I think you need something like this....

    b="HELLO,THERE,WORLD"
    re.findall('[\w]+',b)
    

    Which in Python3 will return

    ['HELLO', 'THERE', 'WORLD']
    
    0 讨论(0)
  • 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]+)$
    
    0 讨论(0)
  • 2020-11-22 11:24

    I know that my answer came late but it happens to me today and I solved it with the following approach:

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

    So the first group (([A-Z]+),)+ will match all the repeated patterns except the final one ([A-Z]+) that will match the final one. and this will be dynamic no matter how many repeated groups in the string.

    0 讨论(0)
  • 2020-11-22 11:24

    Sorry, not Swift, just a proof of concept in the closest language at hand.

    // JavaScript POC. Output:
    // Matches:  ["GOODBYE","CRUEL","WORLD","IM","LEAVING","U","TODAY"]
    
    let str = `GOODBYE,CRUEL,WORLD,IM,LEAVING,U,TODAY`
    let matches = [];
    
    function recurse(str, matches) {
        let regex = /^((,?([A-Z]+))+)$/gm
        let m
        while ((m = regex.exec(str)) !== null) {
            matches.unshift(m[3])
            return str.replace(m[2], '')
        }
        return "bzzt!"
    }
    
    while ((str = recurse(str, matches)) != "bzzt!") ;
    console.log("Matches: ", JSON.stringify(matches))
    

    Note: If you were really going to use this, you would use the position of the match as given by the regex match function, not a string replace.

    0 讨论(0)
  • 2020-11-22 11:28

    You actually have one capture group that will match multiple times. Not multiple capture groups.

    javascript (js) solution:

    let string = "HI,THERE,TOM";
    let myRegexp = /([A-Z]+),?/g;       //modify as you like
    let match = myRegexp.exec(string);  //js function, output described below
    while(match!=null){                 //loops through matches
        console.log(match[1]);          //do whatever you want with each match
        match = myRegexp.exec(bob);     //find next match
    }
    

    Output:

    HI
    THERE
    TOM
    

    Syntax:

    // matched text: match[0]
    // match start: match.index
    // capturing group n: match[n]
    

    As you can see, this will work for any number of matches.

    0 讨论(0)
提交回复
热议问题