将 NSRegularExpression 重写为 RegexKit

发布于 2024-12-15 11:04:02 字数 1346 浏览 1 评论 0原文

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

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

发布评论

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

评论(1

じ违心 2024-12-22 11:04:02

让它以这种方式工作:

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];
}

希望有一天它会对某人有所帮助:)

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 :)

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