Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Regex: regex_replace with named captures/groups
From: Daryle Walker (darylew_at_[hidden])
Date: 2012-01-31 03:21:47


----------------------------------------
> 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.

                                               


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net