NSRegularExpression从numberOfRanges方法给出错误的结果 [英] NSRegularExpression is giving incorrect result from the numberOfRanges method
问题描述
我的代码很小
创建一个新的XCode项目并运行以下代码:
Create a new XCode project and past this code to run:
NSString *stringToCheck = @"## master...origin/master [ahead 1, behind 1]";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[(ahead) (\\d), (behind) (\\d)\\]|\\[(behind) (\\d)\\]|\\[(ahead) (\\d)\\]" options:0 error:nil];
NSArray* matches = [regex matchesInString:stringToCheck options:0 range:NSMakeRange(0, [stringToCheck length])];
NSTextCheckingResult *match = [matches firstObject];
NSUInteger numberOfRanges = [match numberOfRanges];
即时获取= 9的 numberOfRanges
这不应该是5吗?
The numberOfRanges
im getting = 9. But should'nt this be 5 ?
编辑:更多信息
数据可以以3种形式出现
More information the data can come in 3 forms
1. ## master...origin/master [ahead 1, behind 1]
2. ## master...origin/master [ahead 1]
3. ## master...origin/master [behind 1]
我该如何编写代码来说明所有3种情况?不知道找到哪个匹配项,我不知道如何进行。根据下面的一个答案,似乎 [match numberOfRanges]
会返回完整的匹配计数,无论是否找到了该匹配计数。
How do i write code to account for all 3 scenarios? Without knowing which match was found, I do not know how to proceed. Based on one answer below it seems that [match numberOfRanges]
will return the full match count whether or not its found.
如果数据碰巧是#1,则 [match rangeOfIndex:0-4]
方法有效。其他索引失败。
If the data happens to be #1, then the [match rangeOfIndex:0-4]
method works. Other Indexes fail.
如果数据恰好是#2,则 [match rangeOfIndex:5-6]
方法有效。其他失败
If the data happens to be #2, then the [match rangeOfIndex:5-6]
method works. Others fail
如果数据恰好是#3,则 [match rangeOfIndex:7-8]
方法作品。其他人失败了。
If the data happens to be #3, then the [match rangeOfIndex:7-8]
method works. Others fail.
那么我怎么知道哪个组被捕获了,所以我知道要搜索哪个范围?
So how do i tell which group was captured, so i know which range to search?
编辑:基于给出的答案
if ([match rangeAtIndex:1].location != NSNotFound) { // The First capture group
aheadCount = [stringToCheck substringWithRange:[match rangeAtIndex:2]];
behindCount = [stringToCheck substringWithRange:[match rangeAtIndex:4]];
} else if ([match rangeAtIndex:5].location != NSNotFound) { //The second Capture group
aheadCount = [stringToCheck substringWithRange:[match rangeAtIndex:6]];
} else if ([match rangeAtIndex:7].location != NSNotFound) { //The third capture group
behindCount = [stringToCheck substringWithRange:[match rangeAtIndex:8]];
}
推荐答案
每个组,(...)
,您的模式中将产生一个匹配范围,整个模式(索引为0)也会产生一个匹配范围-总共为9。使用or, |
,不会更改此值,因此编号保持一致-每个组都有唯一的编号。
Every group, (...)
, in your pattern will produce a matching range, as will the whole pattern (with index 0) - giving you a total of 9. The use of or, |
, does not change this so numbering remains consistent - every group has a unique number.
在您的示例中,如果您查看所有范围,您都会发现对于匹配5至8, NSRange
位置
的值是 NSNotFound
,因为您的字符串与包含第1组到第4组的第一个选项匹配。
In your example if you examine all the ranges you will find that for matches 5 through 8 the NSRange
location
value is NSNotFound
as your string matches the first alternative which contains groups 1 through 4.
注意:由于每个组通过测试<$而具有唯一的数字c $ c> NSNotFound 您可以确定已匹配的替代项。
Note: As each group has a unique number by testing for NSNotFound
you can determine which alternative has been matched.
这篇关于NSRegularExpression从numberOfRanges方法给出错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!