繁体   English   中英

C#正则表达式到C ++ boost :: regex

[英]C# Regex to C++ boost::regex

我需要匹配以下形式的C ++代码中的字符串

L, N{1, 3}, N{1, 3}, N{1, 3} 

其中在上述伪代码中, L始终是字母(大写或小写) 句号( .字符),而N始终是数字[0-9]

这样明确地,我们可能有B, 999, 999, 999., 8, 8, 8但数字字符的数量总是每个之后的相同,并且是1,2或3个数字长度; 所以D, 23, 232, 23不可能的。

在C#中,我将按以下方式进行匹配

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);

大。 但是,我需要使用boost::regex类似的正boost::regex 我尝试过

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);

但这给了我'w' : unrecognized character escape sequence ,与sd相同。 但是从文档的印象中,我可以毫无问题地使用\\d\\s\\w

我在这里做错了什么?


编辑。 根据上面的评论,我已经切换到std::regex 现在,大概是正则表达式是相同的,以下代码可以编译,但是正则表达式不 匹配...

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, "");

您可以在正则表达式中使用\\w\\s\\d 但是,这不是您在做什么; 您正在尝试使用\\w作为字符串中的字符。 为了使实际字符串中有一个\\后跟w ,您需要转义\\ (当然,与sd相同):

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

从C ++ 11开始,您可以使用原始字符串文字使您的代码与C#版本更加相似:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM