将 NSRegularExpression 重写为 RegexKit
我对 Mac OS X 开发非常陌生(只是第二天)。做了几个月的 iOS 编程,掌握了基础知识,制作了一个小应用程序。然后,当我突然遇到一个问题时,我决定也为 Mac OS X 制作它:NSRegularExpression 仅受 Mac OS X 10.7 或更高版本支持,而我使用的是 10.6.7。经过一番谷歌搜索后,我发现了一个 RegexKit.framework。所以我安装了它,但后来我必须为 RegexKit 框架重写这段代码。
NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"(\\{.*?\\})"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex enumerateMatchesInString:aString options:0 range:NSMakeRange(0, [aString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
[MyParserClass parserWithResponse:[aString substringWithRange:match.range] delegate:self andRequest:request];
}];
任何关于如何使用 RegexKit 做同样的事情的帮助将不胜感激。
编辑: 让它以这种方式工作:
NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *regex = @"(\\{.*?\\})";
NSArray *matches = [aString arrayOfCaptureComponentsMatchedByRegex:regex];
for (NSArray *match in matches) {
[MyParserClass parserWithResponse:[match lastObject] delegate:self andRequest:request];
}
希望有一天它会对某人有所帮助:)
I'm quite new to Mac OS X development(just my second day). Did a couple months of iOS programming, got the basics, made a small app. Then I decided to make it also for Mac OS X when I suddenly encountered a problem: NSRegularExpression is only supported by Mac OS X 10.7 or later and I'm using 10.6.7. After a bit of googling I found a RegexKit.framework. So I installed it, but then I gotta rewrite this piece of code for RegexKit framework.
NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"(\\{.*?\\})"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex enumerateMatchesInString:aString options:0 range:NSMakeRange(0, [aString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
[MyParserClass parserWithResponse:[aString substringWithRange:match.range] delegate:self andRequest:request];
}];
Any help on how can I do the same thing by using RegexKit would be appreciated.
edit:
got it working that way:
NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *regex = @"(\\{.*?\\})";
NSArray *matches = [aString arrayOfCaptureComponentsMatchedByRegex:regex];
for (NSArray *match in matches) {
[MyParserClass parserWithResponse:[match lastObject] delegate:self andRequest:request];
}
hopefully it will help somebody someday :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让它以这种方式工作:
希望有一天它会对某人有所帮助:)
got it working that way:
hopefully it will help somebody someday :)