如何在c ++ boost中编写这些正则表达式?

时间:2012-05-01 12:46:03

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

我想在字符串中做两次替换,我知道如何在php中编写正则表达式,我不熟悉c ++ boost。

// Replace all doubled-up <BR> tags with <P> tags, and remove fonts.
    $string = preg_replace("/<br\/?>[ \r\n\s]*<br\/?>/i", "</p><p>", $string);
    $string = preg_replace("/<\/?font[^>]*>/i", "", $string);

如何在c ++ boost中编写代码?

提前致谢。

1 个答案:

答案 0 :(得分:1)

所有通常的warnings about parsing HTML with regexes都适用。

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
  boost::regex double_br("<br/?>[ \\r\\n\\s]*<br/?>", boost::regex::icase);
  boost::regex fonts("</?font[^>]*>", boost::regex::icase);

  std::string cases[] = {
    "foo<br><br>bar",
    "one<br/><br>two",
    "a<br>   <br/>b",
    "a<br><br>c<br/><br/>d",
    "<font attr=\"value\">w00t!</font>",
    "<font attr=\"value\">hello</font><font>bye</font>",
    ""
  };

  for (std::string *s = cases; *s != ""; ++s) {
    std::cout << *s << ":\n";

    std::string result;
    result = boost::regex_replace(*s, double_br, "</p><p>");
    result = boost::regex_replace(result, fonts, "");

    std::cout << "  - [" << result << "]\n";
  }

  return 0;
}

输出:

foo<br><br>bar:
  - [foo</p><p>bar]
one<br/><br>two:
  - [one</p><p>two]
a<br>   <br/>b:
  - [a</p><p>b]
a<br><br>c<br/><br/>d:
  - [a</p><p>c</p><p>d]
<font attr="value">w00t!</font>:
  - [w00t!]
<font attr="value">hello</font><font>bye</font>:
  - [hellobye]