对于PHP`preg_match_all`函数,什么是Boost :: regex模拟?

时间:2011-07-17 15:58:52

标签: php c++ regex boost boost-regex

所以我有这样的php函数我想翻译成C ++:

protected function htmlTag($content, $tag, $attrName, $attrValue, $valueName)
{
    preg_match_all("#<{$tag}[^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*$valueName=['\"](.+?)['\"][^>]*/?>#i", $content, $matches1);
    preg_match_all("#<{$tag}[^>]*$valueName=['\"](.+?)['\"][^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*/?>#i", $content, $matches2);

    $result = array_merge($matches1[1], $matches2[1]);
    return empty($result)?false:$result[0];
}

使用示例:

            $location = $this->htmlTag($content, 'meta', 'http-equiv', 'X-XRDS-Location', 'content');
            $server   = $this->htmlTag($content, 'link', 'rel', 'openid.server', 'href');
            $delegate = $this->htmlTag($content, 'link', 'rel', 'openid.delegate', 'href');

(内容是$content= curl_exec($curl);)的结果

preg_match_all - 将所有匹配的主题搜索到模式中给出的正则表达式,并按照flags指定的顺序将它们放入匹配项中。找到第一个匹配后,后续搜索将从最后一个匹配结束后继续。

如何使用boost :: regexp翻译它?

1 个答案:

答案 0 :(得分:2)

这样的事情:

boost::optional<std::string> htmlTag(const std::string& content,
    const std::string& tag, const std::string& attrName,
    const std::string& attrValue, const std::string& valueName)
{
    const std::string
        expr1 = boost::format("#<%1[^>]*%2=['\"].*?%3.*?['\"][^>]"
                "*%4=['\"](.+?)['\"][^>]*/?>#i")
                % tag % attrName % attrValue % valueName,
        expr2 = boost::format("#<%1[^>]*%2=['\"](.+?)['\"][^>]*"
                "%3=['\"].*?%4.*?['\"][^>]*/?>#i")
                % tag % attrName % attrValue % valueName;

    boost::match_results<std::string::const_iterator>
        matches1, matches2, result;

    // do the searches (note: these probably need to be loops as shown at the bottom of this page:
    // http://www.boost.org/doc/libs/1_47_0/libs/regex/doc/html/boost_regex/ref/regex_search.html
    if (!regex_search(content, matches1, expr1))
        return boost::none;
    if (!regex_search(content, matches2, expr2))
        return boost::none;

    result = // merge matches1[1] and matches2[1] somehow
    if (result.empty())
        return boost::none;
    else
        return result[0];
}

我确信我的错误有些错误(首先,我认为你需要根据评论反复调用regex_search),但希望你能解决这些细节并发布完成的解决方案。< / p>