Boost.Regex: regex_replace with named captures/groups

Hi @all, does boost's regex_replace not recognize named captures/groups or did I something wrong? std::string date = "2012/01/30"; std::string replace = "\5\g{separator}\4\g{separator}\1\2"; // should be => 30/01/2012 boost::regex rx("^(19|20)(\d\d)(?<separator>[/.])(0[1-9]|1[012])\g{separator}(0[1-9]|[12][0-9]|3[01])$"); std::string replaced = boost::regex_replace(date, rx, replace); The content in "replaced" is now "30\g{separator}01\g{separator}2012" Thanks in advance Best Regards NoRulez

----------------------------------------
From: NoRulez Date: Mon, 30 Jan 2012 21:33:32 +0100
does boost's regex_replace not recognize named captures/groups or did I something wrong?
std::string date = "2012/01/30"; std::string replace = "\5\g{separator}\4\g{separator}\1\2"; // should be => 30/01/2012 boost::regex rx("^(19|20)(\d\d)(?<separator>[/.])(0[1-9]|1[012])\g{separator}(0[1-9]|[12][0-9]|3[01])$");
std::string replaced = boost::regex_replace(date, rx, replace);
The content in "replaced" is now "30\g{separator}01\g{separator}2012"
I don't know too much about regexes, but I believe that they use the backslash character for special purposes. Remember that backslashes are used for escaping in character and string literals in C++, so you need to escape the backslash for boost::regex to see it. e.g. "\5" -> "\\5" etc. The latter makes boost::regex see a backslash followed by a "5" character, while the former is a single character that has the code point of 0x05 (an unprintable control character in ASCII). Daryle W.

does boost's regex_replace not recognize named captures/groups or did I something wrong?
Yes it does, but remember that the C++ compiler gobbles up the \'s before regex gets to se those strings, so use \\ everywhere and you should be fine. HTH, John.
std::string date = "2012/01/30"; std::string replace = "\5\g{separator}\4\g{separator}\1\2"; // should be => 30/01/2012 boost::regex rx("^(19|20)(\d\d)(?<separator>[/.])(0[1-9]|1[012])\g{separator}(0[1-9]|[12][0-9]|3[01])$");
std::string replaced = boost::regex_replace(date, rx, replace);
The content in "replaced" is now "30\g{separator}01\g{separator}2012"
Thanks in advance
Best Regards NoRulez _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hello John,
sorry, I made a copy/paste mistake. The lines should be as followed:
FALSE:
std::string data = "Today is the 2012-01-31";
std::string replace = "\\5\\g{sep}\\4\\g{sep}\\1\\2";
std::string pattern = "(19|20)(\\d\\d)(?<sep>[- /.])(0[1-9]|1[012])\\g{sep}(0[1-9]|[12][0-9]|3[01])";
boost::regex rx(pattern);
std::string replaced = boost::regex_replace(data, rx, replace);
RESULT: "Today is the 31g{sep}01g{sep}2012"
If I use numbers instead of named groups as followed it works:
CORRECT:
std::string data = "Today is the 2012-01-31";
std::string replace = "\\5\\3\\4\\3\\1\\2";
std::string pattern = "(19|20)(\\d\\d)([- /.])(0[1-9]|1[012])\\3(0[1-9]|[12][0-9]|3[01])";
boost::regex rx(pattern);
std::string replaced = boost::regex_replace(data, rx, replace);
RESULT: "Today is the 31-01-2012"
Best Regards
NoRulez
Am 31. Jan 2012 um 09:41 schrieb John Maddock
does boost's regex_replace not recognize named captures/groups or did I something wrong?
Yes it does, but remember that the C++ compiler gobbles up the \'s before regex gets to se those strings, so use \\ everywhere and you should be fine.
HTH, John.
std::string date = "2012/01/30"; std::string replace = "\5\g{separator}\4\g{separator}\1\2"; // should be => 30/01/2012 boost::regex rx("^(19|20)(\d\d)(?<separator>[/.])(0[1-9]|1[012])\g{separator}(0[1-9]|[12][0-9]|3[01])$");
std::string replaced = boost::regex_replace(date, rx, replace);
The content in "replaced" is now "30\g{separator}01\g{separator}2012"
Thanks in advance
Best Regards NoRulez _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Daryle Walker
-
John Maddock
-
NoRulez
-
norulez@me.com