【发布时间】:2014-01-23 20:09:53
【问题描述】:
我有一个包含 HTML 页面的 NSString。我会按 id 提取特定标签,按名称提取(此标签的)特定属性。 我以正则表达式为例:
NSError *error = nil;
NSString *authParameter = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<form id=\"theIdOfForm\"*.*method=\"post\">" options:NSRegularExpressionCaseInsensitive error:&error];
if (error == nil) {
NSTextCheckingResult *match = [regex firstMatchInString:aHtmlFirstResponse options:0 range:NSMakeRange(0, [aHtmlFirstResponse length])];
if (match) {
NSString *formHtmlTag = [aHtmlFirstResponse substringWithRange:[match rangeAtIndex:0]];
}
但是,如何从该标签中按名称提取属性? 什么是一种优雅而有效的方法来获得这个? 我想避免导入一些 HTML 解析器库。
【问题讨论】:
-
NSTextCheckingResult 为您提供匹配范围,允许您指定要从中提取片段的子字符串。在某些情况下,您需要使用其他正则表达式来分解它。我更喜欢使用 RegexKitLite,它使表达式解析和结果提取变得容易一些。
标签: html ios objective-c regex parsing