此代码没有返回任何内容,我是否以错误的方式转义了 w 字符?
http://liveworkspace.org/code/3bRWOJ $38
#include <iostream>
#include <regex>
using namespace std;
int main()
{
const char *reg_esp = "\w"; // List of separator characters.
// this can be done using raw string literals:
// const char *reg_esp = R"([ ,.\t\n;:])";
std::regex rgx(reg_esp); // 'regex' is an instance of the template class
// 'basic_regex' with argument of type 'char'.
std::cmatch match; // 'cmatch' is an instance of the template class
// 'match_results' with argument of type 'const char *'.
const char *target = "Unseen University - Ankh-Morpork";
// Identifies all words of 'target' separated by characters of 'reg_esp'.
if (std::regex_search(target, match, rgx)) {
// If words separated by specified characters are present.
const size_t n = match.size();
for (size_t a = 0; a < n; a++) {
std::string str (match[a].first, match[a].second);
std::cout << str << "\n";
}
}
return 0;
}
最佳答案
正则表达式应包含\w
,由两个字符\
和w
组成,因此您的C++源代码应包含"\\w"
因为您需要转义反斜杠。
关于c++ - Regex\w 在 C++11 中转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15998482/