我有这个代码片段,我从我在网上找到的东西派生出来:
std::regex pieces_regex("([a-z]+)\\.([a-z]+)", std::regex_constants::basic);
std::smatch pieces_match;
std::string fname = "foo.txt";
cout << " fname " << fname << endl;
if (std::regex_match(fname, pieces_match, pieces_regex)) {
std::cout << fname << '\n';
for (size_t i = 0; i < pieces_match.size(); ++i) {
std::ssub_match sub_match = pieces_match[i];
std::string piece = sub_match.str();
std::cout << " submatch " << i << ": " << piece << '\n';
}
}
pieces_regex构造函数不能与我的编译器一起工作,所以我不得不改变
行std::regex pieces_regex("([a-z]+)\\.([a-z]+)");
到
std::regex pieces_regex("([a-z]+)\\.([a-z]+)", std::regex_constants::basic);
我的regex_match行现在没有返回任何内容,pieces_match的大小为零。我尝试向regex_match添加几个不同的标志,比如basic或match_any,但它没有帮助。 原始代码可以在这里找到: http://en.cppreference.com/w/cpp/regex/regex_match
任何人都知道我错过了什么?
谢谢!
答案 0 :(得分:0)
标准正则表达式(没有第二个'flags'参数)使用ECMA脚本作为默认值。试试这个:
std::regex pieces_regex("([a-z]+)\\.([a-z]+)", std::regex_constants::ECMAScript);
// ^
你应该看到一些结果......
编辑:由于您在使用ECMA脚本时遇到问题:您选择了语法:extended
,awk
和egrep
所有人都会按预期吞下您的表达式...