简体   繁体   English

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

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

I have the requirement to match strings in a C++ code of the form 我需要匹配以下形式的C ++代码中的字符串

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] . 其中在上述伪代码中, L始终是字母(大写或小写) 句号( .字符),而N始终是数字[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; 这样明确地,我们可能有B, 999, 999, 999., 8, 8, 8但数字字符的数量总是每个之后的相同,并且是1,2或3个数字长度; so D, 23, 232, 23 is not possible. 所以D, 23, 232, 23不可能的。

In C# I would match this as follows 在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);

Great. 大。 However, I need a similar regex using boost::regex . 但是,我需要使用boost::regex类似的正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 . 但这给了我'w' : unrecognized character escape sequence ,与sd相同。 But from the documentation I was under the impression I can use \\d , \\s and \\w without issue. 但是从文档的印象中,我可以毫无问题地使用\\d\\s\\w

What am I doing wrong here? 我在这里做错了什么?


Edit. 编辑。 I have switched to std::regex as-per a comment above. 根据上面的评论,我已经切换到std::regex 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, "");

You can use \\w , \\s , and \\d in your regular expressions. 您可以在正则表达式中使用\\w\\s\\d However, that's not what you're doing; 但是,这不是您在做什么; you're trying to use \\w as a character in the string. 您正在尝试使用\\w作为字符串中的字符。 For there to be a \\ followed by a w in the actual string, you need to escape the \\ (same for s and d , of course): 为了使实际字符串中有一个\\后跟w ,您需要转义\\ (当然,与sd相同):

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

As of C++11, you can use raw string literals to make your code even more similar to the C# version: 从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