本文介绍了C ++中的正则表达式和双反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取格式为

People list
[Jane]
Female
31
...

,对于我要遍历的每一行,查找包含"[...]"的行例如,[简]

and for each line I want to loop through and find the line that contains "[...]"For example, [Jane]

我想出了正则表达式

我使用regex101.com对其进行了测试.但是,当我尝试在代码中使用它时,它无法与任何内容匹配.这是我的代码:

which I tested that it works using regex101.com.However, when I try to use that in my code, it fails to match with anything.Here's my code:

void Jane::JaneProfile() {
    // read each line, for each [title], add the next lines into its array
    std::smatch matches;
    for(int i = 0; i < m_numberOfLines; i++) { // #lines in text file
        std::regex pat ("(^\[\w+\]$)");
        if(regex_search(m_lines.at(i), matches, pat)) {
            std::cout << "smatch " << matches.str(0) << std::endl;
            std::cout << "smatch.size() = " << matches.size() << std::endl;

        } else
            std::cout << "wth" << std::endl;
    }
}

当我运行这段代码时,所有行都进入else循环,没有任何匹配...

When I run this code, all the lines go to the else loop and nothing matches...

我搜索了答案,但是当我看到对于C ++必须使用双反斜杠而不是一个反斜杠进行转义时,我感到困惑.但是即使我使用了双反斜杠,该代码也不适用于我的代码..我哪里出问题了?

I searched up for answers, but I got confused when I saw that for C++ you have to use double backslashes instead one backslash to escape... But it didn't work for my code even when I used double backslashes...Where did I go wrong?

顺便说一句,我正在使用基于(桌面)Qt 5.5.1(Clang 6.1(Apple),64位)的Qt Creator 3.6.0

By the way, I'm using Qt Creator 3.6.0 Based on (Desktop) Qt 5.5.1 (Clang 6.1 (Apple), 64 bit)

-编辑----

我尝试做:

std::regex pat (R"(^\[\\w+\]$)");

但是我说错了

我已经有 #include< regex> ,但是我还需要添加其他内容吗?

I already have #include <regex> but do I need to include something else?

推荐答案

要么转义反斜杠,要么使用带有正则表达式中不会出现的前缀的原始字符版本:

Either escape the backslashes or use the raw character version with a prefix that won't appear in the regex:

转义:

std::regex pat("^\\[\\w+\\]$");

原始字符串:

std::regex pat(R"regex(^\[\w+\]$)regex");

工作演示(改编自OP的发布代码):

working demo (adapted from OPs posted code):

#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    auto test_data =
    "People list\n"
    "[Jane]\n"
    "Female\n"
    "31";

    // initialise test data
    std::istringstream source(test_data);
    std::string buffer;
    std::vector<std::string> lines;
    while (std::getline(source, buffer)) {
        lines.push_back(std::move(buffer));
    }

    // test the regex

    // read each line, for each [title], add the next lines into its array
    std::smatch matches;
    for(int i = 0; i < lines.size(); ++i) { // #lines in text file
        static const std::regex pat ("(^\\[\\w+\\]$)");
        if(regex_search(lines.at(i), matches, pat)) {
            std::cout << "smatch " << matches.str() << std::endl;
            std::cout << "smatch.size() = " << matches.size() << std::endl;
        } else
            std::cout << "wth" << std::endl;
    }

    return 0;
}

预期输出:

wth
smatch [Jane]
smatch.size() = 2
wth
wth

这篇关于C ++中的正则表达式和双反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:02