Objective-C 中的 NSRegularExpression

发布于 2024-12-09 11:13:24 字数 1269 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫 2024-12-16 11:13:24

重复的反向引用仅捕获最后一次重复。您的正则表达式确实匹配最后一行中的所有四个实例,但它会用下一个匹配项覆盖每个匹配项,仅在末尾留下 [04:39.50]

解决方案:重复一个非捕获组,并将重复的结果放入捕获组中:

((?:\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\])+)(.+)

当然,最后一次重复您仍然只能访问 $2$4 - 但是这是正则表达式的一般限制。如果您需要单独访问每个匹配项,直至分钟/秒/帧部分,则

((?:\\[\\d{2}:\\d{2}\\.\\d{2}\\])+)(.+)

首先使用匹配每一行,然后在迭代中将第二个正则表达式应用于 $1 以提取分钟等。 :

\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\]

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:

((?:\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\])+)(.+)

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 use

((?:\\[\\d{2}:\\d{2}\\.\\d{2}\\])+)(.+)

to match each line first, and then apply a second regex to $1 in iteration to extract the minutes etc.:

\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文