Hi,
I recently downloaded the boost regex library and am trying to use
the search and replace (regex_merge) API. But I am not getting the
expected result.
Here is what I am trying to do. I am trying to replace a single
quoted string with something. But, the catch here is that the string
in single quotes can have zero or many 2 consecutive single quotes.
For example:
Input (Ignore the angle brackets>:
To be replaced by 'XXX'. So the output that I want:
Here is the regular expression that I wrote in Perl:
/(\'[^']*(\'\')*[^']*\')/
I get the expected result in Perl.
So, to test this with regex library I wrote a sample program to use
the regex_merge API and try the replace functionality.
---------------------------replace.cc--------------------------------
#include
#include <string>
#include <iostream>
#include <sstream>
#include <iterator>
using namespace std;
using namespace boost;
const std::string formatstr("XXX");
std::string processexp(string resp)
{
regex expression("(\'[^']*(\'\')*[^']*\')");
std::string result;
std::ostringstream t;
std::ostream_iterator oi(t);
cout << "Response:" << resp << endl;
result = boost::regex_merge(resp, expression, formatstr,
boost::format_all);
return result;
}
int main(int argc)
{
std::string res;
res = processexp("foo='string with '' consecutive ''
single ''quotes' bar='string with no single quotes'");
cout << "results=" << res << endl;
return 0;
}
---------------------------------------------------------------------
The output I get is:
foo=REPLACEDREPLACED bar=REPLACED
I tried several ways, but couldn't get the regular expression to
work to return the expected result.
I WOULD really appreciate if someone could help me.
Thanks in advance,
Sameer