Objective-C 中的 NSRegularExpression
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\])+(.+)" options:NSRegularExpressionAllowCommentsAndWhitespace error:&error];
[regex enumerateMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, [self length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
[*lyricObject addObject:[self substringWithRange:[match rangeAtIndex:5]]];
NSLog(@"%@",[self substringWithRange:[match rangeAtIndex:1]]);
[*stamp addObject:[NSString stringWithFormat:@"%d", ([[self substringWithRange:[match rangeAtIndex:2]] intValue] * 60 + [[self substringWithRange:[match rangeAtIndex:3]] intValue] ) * 100 + [[self substringWithRange:[match rangeAtIndex:4]] intValue]]];
}];
就像输入字符串(self)上面的代码一样:
[04:30.50]There are pepole dying
[04:32.50]If you care enough for the living
[04:35.50]Make a better place for you and for me
[04:51.50][04:45.50][04:43.50][04:39.50]You and for me
我想获取 [04:51.50][04:45.50][04:43.50][04:39.50]
的 for groups 但我只能在 [04:39.50]
上获取最后一个
NSRegularExpression
是否只能在我搜索时获取最后一组(($1)($2)($3)){2}
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\])+(.+)" options:NSRegularExpressionAllowCommentsAndWhitespace error:&error];
[regex enumerateMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, [self length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
[*lyricObject addObject:[self substringWithRange:[match rangeAtIndex:5]]];
NSLog(@"%@",[self substringWithRange:[match rangeAtIndex:1]]);
[*stamp addObject:[NSString stringWithFormat:@"%d", ([[self substringWithRange:[match rangeAtIndex:2]] intValue] * 60 + [[self substringWithRange:[match rangeAtIndex:3]] intValue] ) * 100 + [[self substringWithRange:[match rangeAtIndex:4]] intValue]]];
}];
Just like the code above the input string(self) is:
[04:30.50]There are pepole dying
[04:32.50]If you care enough for the living
[04:35.50]Make a better place for you and for me
[04:51.50][04:45.50][04:43.50][04:39.50]You and for me
and I want to get the for groups for [04:51.50][04:45.50][04:43.50][04:39.50]
but I can only get the last on [04:39.50]
Is the NSRegularExpression
can only get the last group when I search (($1)($2)($3)){2}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重复的反向引用仅捕获最后一次重复。您的正则表达式确实匹配最后一行中的所有四个实例,但它会用下一个匹配项覆盖每个匹配项,仅在末尾留下
[04:39.50]
。解决方案:重复一个非捕获组,并将重复的结果放入捕获组中:
当然,最后一次重复您仍然只能访问
$2
到$4
- 但是这是正则表达式的一般限制。如果您需要单独访问每个匹配项,直至分钟/秒/帧部分,则首先使用匹配每一行,然后在迭代中将第二个正则表达式应用于
$1
以提取分钟等。 :A repeated backreference only captures the last repetition. Your regex does match all four instances in that last line, but it overwrites each match with the next one, leaving only
[04:39.50]
at the end.Solution: Repeat a non-capturing group, and put the repeated result into the capturing group:
You still can only access
$2
through$4
for the last repetition, of course - but this is a general limitation of regexes. If you need to access each match individually, down to the minutes/seconds/frames parts, then useto match each line first, and then apply a second regex to
$1
in iteration to extract the minutes etc.: