1

I want to do two replacement in a string, and i know how to write the regex in php, curently i am not familiar with 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);

how to write the code in c++ boost?

Thanks in advance.

luyi0619
  • 313
  • 3
  • 8
  • 3
    Well, did you try yet? What errors/problems are you getting? – Mat May 01 '12 at 12:47
  • These regexes will work but you'll need to double escape your regex escape characters (e.g. \r should be \\r) you'll need this since \ happens to also be the string escape character in C++. – Benj May 01 '12 at 12:55
  • I used double escape characters and tried, but it did not replace anything ... – luyi0619 May 01 '12 at 13:05

1 Answers1

1

All the usual warnings about parsing HTML with regexes apply.

#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;
}

Output:

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]
Community
  • 1
  • 1
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245