gpt4 book ai didi

c++ - 如何将 C# 风格的正则表达式转换为 C++ 风格

转载 作者:行者123 更新时间:2023-11-30 01:15:44 25 4
gpt4 key购买 nike

我试图通过正则表达式解析一个冗长的字符串,我尝试使用以下 RE 和此链接中提到的文本来完成 http://regexr.com/3a7uf

但是当我尝试使用相同的 RE 解析 C++ 中的文本时,编译时警告和输出不符合预期。

请告知如何将此 RE 排序为一种格式,以便在 C++ 程序中解析文本成为可能。

代码是这样的:

std::string str = "root     21015  0.0  0.0      0     0 ?        S    "
"16:07 0:00 [kworker/5:0]\n root 21095 0.0 0.0 "
" 0 0 ? S 16:08 0:00 [kworker/2:2]\n "
"root 21097 0.0 0.0 0 0 ? S 16:08 0:00 ["
"kworker/u16:4]\n ashish 21103 17.1 1.2 591536 106056"
" ? Sl 16:12 0:01 /usr/lib/firefox/firefox";

std::regex firefox ("[0-9].\..*.firefox");

std::smatch sm;
std::regex_match (str, sm, firefox);
std::cout << "number of matches: " << sm.size() << std::endl;

std::cout << "matches were: " << std::endl;
for (unsigned int i = 0; i < sm.size(); i++)
{
std::cout << "[" << sm[i] << "]";
}

编译时警告如下:

warning: unknown escape sequence: '\\.'
regex firefox ("[0-9].\\..*.firefox");

输出如下:

number of matches: 0

matches were:

最佳答案

您需要使用 double escapes在这里。

[0-9].\\..*.firefox

关于c++ - 如何将 C# 风格的正则表达式转换为 C++ 风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27991969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com