【问题标题】:Extract an html tag and attribute from NSString从 NSString 中提取一个 html 标签和属性
【发布时间】: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


【解决方案1】:

你可以使用

https://github.com/zootreeves/Objective-C-HMTL-Parser

我已将它用于我的一个项目。

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 2011-10-02
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多