【发布时间】:2013-05-18 16:26:19
【问题描述】:
所以我的教授给了我一个在 c++ 中使用正则表达式的工作。
所以我尝试在 eclipse 中编写我的代码(我使用的是 linux (ubuntu 12.04))。
所以我拿了代码:
// regex_search example
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("this subject has a submarine as a subsequence");
std::smatch m;
std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
std::cout << "Target sequence: " << s << std::endl;
std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
std::cout << "The following matches and submatches were found:" << std::endl;
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}
如您所见,这是一个使用正则表达式的简单代码。
所以我尝试构建它并且 eclipse 给了我一个错误:
Type 'smatch' could not be resolved
还有:
Type 'std::regex' could not be resolved
有什么问题?
我尝试添加标志 -std=c++0x 在合适的位置(properties->c/c++ build ->Miscellaneous),什么也没有发生。
也许我做错了?
也许我必须像在 pthread 中一样添加指向库的链接?
【问题讨论】:
-
你运行的是什么版本的 GCC?
-
你可能在那个版本的 Ubuntu 上使用 gcc 4.7.2(或者之前的那个)?该版本未实现正则表达式。 (不确定他们现在已经走了多远。我看到的任何活动的最后证据是 1 月份邮件列表中的一些模糊的 cmets。
-
考虑
Boost.Regex。问问你的教授,他们应该知道他们打算让你使用什么。 -
要么将库更改为支持更好的库,要么使用Boost regex(应该非常兼容)。
标签: c++ regex eclipse compiler-errors eclipse-cdt