问题
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 pattern
^(?:([A-Z]+),?)+$
What I want it to do is, capture every single word, so that Group 1 is : \"HELLO\", Group 2 is \"THERE\" and Group 3 is \"WORLD\" What my regex is actually capturing only the last one, which is \"WORLD\".
I\'m testing my regular expression here and I want to use it with Swift (maybe there\'s a way in Swift to get intermediate results somehow, so that I can use them?)
UPDATE: I don\'t want to use split
. I just need to now how to capture all the groups that have matched the pattern, not only the last one.
回答1:
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]+)$
回答2:
I think you need something like this....
b="HELLO,THERE,WORLD"
re.findall('[\w]+',b)
Which in Python3 will return
['HELLO', 'THERE', 'WORLD']
回答3:
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
来源:https://stackoverflow.com/questions/37003623/how-to-capture-multiple-repeated-groups