使用C 中的特殊格式拆分字符串

Split a string a string using special formatting in c++

本文关键字:格式 拆分 字符串 使用      更新时间:2023-10-16

我正在尝试分开这样的字符串:

" aaaaaaaa" 1 " bbbbbbbbb"

随附的引号,以获取AAAAAAAAA BBBBBBBBB。

我找到了不同的方法来划分字符串,但引号和斜线的存在引起了许多问题。

例如,如果我使用string.find,我不能使用string.find(" 1 ");

有人知道如何帮助我吗?谢谢

#include <iostream>
#include <string>
#include <regex>
int main()
{
    // build a test string and display it
    auto str = std::string(R"text("aaaaaaaa"1"bbbbbbbbb")text");
    std::cout << "input : " << str << std::endl;
    // build the regex to extract two quoted strings separated by "1"
    std::regex re(R"regex("(.*?)"\1\"(.*?)")regex");
    std::smatch match;
    // perform the match
    if (std::regex_match(str, match, re))
    {
        // print captured groups on success
        std::cout << "matched : " << match[1] << " and " << match[2] << std::endl;
    }
}

预期结果:

input : "aaaaaaaa"1"bbbbbbbbb"
matched : aaaaaaaa and bbbbbbbbb