1

当我必须在 PHP 中创建 CMS 时,我创建了如下所示的简单unescape html函数:

function unescape($s) {
    $s= preg_replace('/%u(....)/', '&#x$1;', $s);
    $s= preg_replace('/%(..)/', '&#x$1;', $s);
return $s;
}

如何使用Boost.Regex将其翻译成 C++ ?

4

1 回答 1

1

我猜它看起来有点像这样:

std::string unescape(const std::string s)
{
  std::string temp = boost::regex_replace(s, "%u(....)", "&#x$1;", boost::match_default);
  temp = boost::regex_replace(temp, "%u(..)", "&#x$1;", boost::match_default);
  return temp;
}

但我认为.(DOT) 应该只匹配十六进制值,在这种情况下,我会选择这样的东西:

std::string unescape(const std::string s)
{
  return boost::regex_replace(s, "%u([0-9a-fA-F]{2}|[0-9a-fA-F]{4})", "&#x$1;",
                              boost::match_default);
}

(请注意,我没有对此进行测试!)

于 2011-07-24T19:00:06.363 回答