我试图通过正则表达式解析一个冗长的字符串,我尝试使用此链接中提到的以下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:
答案 0 :(得分:3)
您需要在此处使用double escapes。
[0-9].\\..*.firefox
答案 1 :(得分:1)
这让我觉得只是处理字符串的问题。你可以尝试
std::regex firefox (@"[0-9].\..*.firefox");
要表明它是字符串文字,或者不支持该语法,请尝试
std::regex firefox ("[0-9].\\..*.firefox");
这就是说你真的想要字符串中的\
字符,而不是转义期。
从下面的评论中看起来好像C#语法不正确,或者句点是连接的(如PHP?),但是它们不会在正则表达式they are placeholders中连接。
std::regex firefox ("[0-9]+[.][0-9]+[^.]*firefox");
您可以在上面的示例中突出显示您想要匹配的内容(完全开始)吗?我无法确定您希望匹配开始的位置,但如果您尝试查找数字和句点,则上述内容将从1.2
开始。
答案 2 :(得分:1)
你将不得不逃避反斜杠,使它们成为有效的C ++字符串。例如,尝试:
std::regex firefox ("[0-9].\\..*.firefox");
答案 3 :(得分:0)
感谢您的回复。表达本身没有任何问题。
我只是使用以下语法来创建正则表达式,它运行良好。
std::regex otherProcessRegEx ("[0-9][0-9][:.:].*.[a-z].|[0-9][:.:].*.[a-z]",
std::regex_constants::ECMAScript |
std::regex_constants::icase);
使用c ++ 11引入了这些http://www.johndcook.com/blog/cpp_regex/种不同类型的RegEx,这些类型需要指定。
:)