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

标签 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中编写代码?

提前致谢。

最佳答案

所有常见的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]

关于php - 如何在 C++ boost 中编写这些正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10397811/

相关文章:

php - Codeigniter MYSQL 连接作为单个记录返回?

c++ - 字段类型不完整,从多个文件编译

python - 从python中的字符串中提取年份

objective-c - Cocoa 中这个简单的正则表达式有什么问题

php - 使用正则表达式的深度(无限)嵌套拆分词

php - CentOS 6.7 php 5.6 Chef

php - 自动邮件列表 - PHP

php - 在 mysql 中,一行总是在最上面

c++ - 类型特征 : Check if class have specific function (maybe inherit)

c++ - Boost::spirit 解析一个 float 并格式化它?