Objective-C, regular expression match repetition

混江龙づ霸主 提交于 2020-01-05 07:34:29

问题


I found a problem in regular expression to match all group repetition.

This is a simple example:

NSString *string = @"A1BA2BA3BC";
NSString *pattern = @"(A[^AB]+B)+C";

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *array = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];

Returning array have one element which contains two ranges, whole input string and last captured group "A3B". First two groups, "A1B" and "A2B", are not captured as I expected.

I've tried all from greedy to lazy matching.


回答1:


A Quantifier Does not Spawn New Capture Groups

Except in .NET, which has CaptureCollections, adding a quantifier to a capture group does not create more captures. The group number stays the same (in your case, Group 1), and the content returned is the last capture of the group.

Reference

Everything about Regex Capture Groups (see Generating New Capture Groups Automatically)

Iterating the Groups

If you wanted to match all the substrings while still validating that they are in a valid string (composed of such groups and ending in C), you could use:

 A[^AB]+B(?=(?:A[^AB]+B)*C)

The whole string, of course, would be

^(?:A[^AB]+B)+C$

To iterate the substrings: something like

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"A[^AB]+B(?=(?:A[^AB]+B)*C)" options:0 error:&error];
NSArray *matches = [regex matchesInString:subject options:0 range:NSMakeRange(0, [subject length])];
NSUInteger matchCount = [matches count];
if (matchCount) {
    for (NSUInteger matchIdx = 0; matchIdx < matchCount; matchIdx++) {
        NSTextCheckingResult *match = [matches objectAtIndex:matchIdx];
        NSRange matchRange = [match range];
        NSString *result = [subject substringWithRange:matchRange];
    }
}
else {  // Nah... No matches.
     }


来源:https://stackoverflow.com/questions/24631571/objective-c-regular-expression-match-repetition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!