Monday, 15 February 2010

C# Regex to C++ boost::regex -



C# Regex to C++ boost::regex -

i have requirement match strings in c++ code of form

l, n{1, 3}, n{1, 3}, n{1, 3}

where in above pseudo-code, l letter (upper or lower case) or fullstop (. character) , n numeric [0-9].

so explicitly, might have b, 999, 999, 999 or ., 8, 8, 8 number of numeric characters same after each , , either 1, 2 or 3 digits in length; d, 23, 232, 23 not possible.

in c# match 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, need similar regex using boost::regex. 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 giving me 'w' : unrecognized character escape sequence , same s , d. documentation under impression can utilize \d, \s , \w without issue.

what doing wrong here?

edit. have switched std::regex as-per comment above. now, presumably regex same , next compiles 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, "");

you can utilize \w, \s, , \d in regular expressions. however, that's not you're doing; you're trying utilize \w character in string. there \ followed w in actual string, need escape \ (same s , d, of course):

boost::regex regex("[\\.\\w],\\s*\\d{1,3},\\s*\\d{1,3},\\s*\\d{1,3}");

as of c++11, can utilize raw string literals create code more similar c# version:

boost::regex regex(r"del([\.\w],\s*\d{1,3},\s*\d{1,3},\s*\d{1,3})del");

c# c++ boost boost-regex

No comments:

Post a Comment