c++ - Regex\w 在 C++11 中转义

标签 c++ regex c++11

此代码没有返回任何内容,我是否以错误的方式转义了 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/

相关文章:

regex - 如何修剪线包括特定字符串与 sed

regex - 如何在python3中组合两个re.compile正则表达式?

c++ - 将参数传递给构造函数和成员函数时 move 或复制

c++ - 如何使用 STL 正确跟踪地址?

c++ - Matlab中的USRPs并行控制

c++ - stringstream 在转换时失去精度

c++ - boost::stacktrace::safe_dump_to 输出大小

c++ - 另一个问题 : Passing argument by reference

javascript - 正则表达式不工作

c++ - 全局静态初始化线程