正则表达式在C ++ 11中的转义 [英] Regex \w escaping in C++11

查看:159
本文介绍了正则表达式在C ++ 11中的转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码未返回任何内容,我是否以错误的方式转义了w字符?

This code isn't returning anything, am I escaping the w character the wrong way??

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 ,因为您需要转义反斜杠。

The regular expression should contain \w, consisting of two characters, \ and w, hence your C++ source code should contain "\\w" as you need to escape the backslash.

这篇关于正则表达式在C ++ 11中的转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆