Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C中用于查找HTML锚列表的正则表达式_Html_Objective C_Regex_Nsregularexpression - Fatal编程技术网

Objective-C中用于查找HTML锚列表的正则表达式

Objective-C中用于查找HTML锚列表的正则表达式,html,objective-c,regex,nsregularexpression,Html,Objective C,Regex,Nsregularexpression,我开始使用Objective-C进行开发,但在找到正确的正则表达式以在HTML文档中列出锚点时遇到了问题 示例:我有以下HTML代码: <ul> <li><a class="class1" href="/document1.html"></li> <li><a class="class1" href="/document2.html"></li> <li><a class

我开始使用Objective-C进行开发,但在找到正确的正则表达式以在HTML文档中列出锚点时遇到了问题

示例:我有以下HTML代码:

<ul>
    <li><a class="class1" href="/document1.html"></li>
    <li><a class="class1" href="/document2.html"></li>
    <li><a class="class1" href="/document3.html"></li>
</ul>

我怎样才能为它生成一个好的正则表达式呢?

在正则表达式中正确地实现这一点是很复杂的,有HTML允许的所有通用性。最好使用HTML解析器,例如Hpple。看雷·温德里奇的

但是,如果您只对某些特殊情况感兴趣(例如,
href
始终使用双引号),您可以执行以下操作:

NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"<a\\s[^>]*(?<=\\s)href\\s*=\\s*\"(.*?)\".*?>"
                                                  options:NSRegularExpressionCaseInsensitive
                                                    error:&error];
NSRegularExpression*regex;
正则表达式=[NSRegularExpression regular expression with pattern:@“]*(?”
选项:nsregularexpressioncase不敏感
错误:&错误];
这里有很多限制,但这可能是一个起点。对于更一般的东西,您确实应该使用HTML解析器,而不是正则表达式。

可能的重复
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"<a\\s[^>]*(?<=\\s)href\\s*=\\s*\"(.*?)\".*?>"
                                                  options:NSRegularExpressionCaseInsensitive
                                                    error:&error];