I have the requirement to match strings in a C++ code of the form
L, N{1, 3}, N{1, 3}, N{1, 3}
where in the above pseudo-code, L
is always a letter (upper or lower case) or a fullstop (.
character) and N
is always numeric [0-9]
.
So explicitly, we might have B, 999, 999, 999
or ., 8, 8, 8
but the number of numeric characters is always the same after each ,
and is either 1, 2 or 3 digits in length; so D, 23, 232, 23
is not possible.
In C# I would match this as follows
string s = " B,801, 801, 801 other stuff";
Regex reg = new Regex(@"[\.\w],\s*\d{1,3},\s*\d{1,3},\s*\d{1,3}");
Match m = reg.Match(s);
Great. However, I need a similar regex using boost::regex
. I have attempted
std::string s = " B,801, 801, 801 other stuff";
boost::regex regex("[\\.\w],\s*\d{1,3},\s*\d{1,3},\s*\d{1,3}");
boost::match_results<std::string::const_iterator> results;
boost::regex_match(s, results, regex);
but this is giving me 'w' : unrecognized character escape sequence
and the same for s
and d
. But from the documentation I was under the impression I can use \d
, \s
and \w
without issue.
What am I doing wrong here?
Edit. I have switched to std::regex
as-per a comment above. Now, presumably the regex is the same and the following compiles but the regex does not match...
std::string p = "XX";
std::string s = " B,801, 801, 801 other stuff";
std::regex regex(R"del([\.\w],\s*\d{1,3},\s*\d{1,3},\s*\d{1,3})del");
if (std::regex_match(s, regex))
p = std::regex_replace(s, regex, "");