在 C++ 中使用特殊格式将字符串拆分为字符串
Split a string a string using special formatting in c++
我正在尝试像这样拆分字符串:
"aaaaaaaa"\1\"bbbbbbbbb"
包含引号,以获得 aaaaaaaa 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"\"bbbbbbbbb")text");
std::cout << "input : " << str << std::endl;
// build the regex to extract two quoted strings separated by "\"
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"\"bbbbbbbbb"
matched : aaaaaaaa and bbbbbbbbb
我正在尝试像这样拆分字符串:
"aaaaaaaa"\1\"bbbbbbbbb"
包含引号,以获得 aaaaaaaa 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"\"bbbbbbbbb")text");
std::cout << "input : " << str << std::endl;
// build the regex to extract two quoted strings separated by "\"
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"\"bbbbbbbbb"
matched : aaaaaaaa and bbbbbbbbb