boost::regex_replaceで置換

"と'の前にバックスラッシュをつけたい。
\\\\と4つ連続で書かないと、置換できなかった。まさか4つとは。


regex_replace.cpp

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

using namespace std;
using namespace boost;

int main(int argc, char* argv[]){
  string str = "asdf\"jjjjj\"hog'ehoge";
  cout << str << endl;
  
  string result = regex_replace(str, regex("[\"\']"), "\\\\$0");
  cout << result << endl;
  return 0;
}


Makefile

SRC = regex_replace.cpp
DST = regex_replace

prefix=/opt/local
INCPATH=/opt/local/include
LIBPATH=/opt/local/lib

INCLUDE=-I$(INCPATH)/boost
LIBS=$(LIBPATH)/libboost_regex-mt.a

all:
        g++ -O $(SRC) -o $(DST) $(INCLUDE) $(LIBS)
asdf"jjjjj"hog'ehoge
asdf\"jjjjj\"hog\'ehoge