Unexpected result in parsing a regular expression
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>: <foo='string with '' consecutive '' single ''quotes' bar='string with no single quotes'> To be replaced by 'XXX'. So the output that I want: <foo=XXX bar=XXX> 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 <boost/regex.hpp> #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<char, char> 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
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>: <foo='string with '' consecutive '' single ''quotes' bar='string with no single quotes'> To be replaced by 'XXX'. So the output that I want: <foo=XXX bar=XXX>
Here is the regular expression that I wrote in Perl: /(\'[^']*(\'\')*[^']*\')/
I get the expected result in Perl.
Actually are you really sure that this works in perl: IMO it shouldn't, since it can never match a string with more than *one* '' embedded in it, I think you should be using: /((?:\'[^']*\')+)/ John Maddock http://ourworld.compuserve.com/homepages/john_maddock/index.htm
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 <boost/regex.hpp> #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<char, char> 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
Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com>
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
participants (2)
-
abhinkar
-
John Maddock