如何从字符串中提取多个子字符串?
How to extract multiple substrings from a string?
我原来的字符串是这样的<Information name="Identify" id="IdentifyButton" type="button"/>
如何从这个字符串中提取 3 个子字符串 string name_part = Identify
、string id_part="IdentifyButton"
、type_part="button"
假设您不想使用 third-party XML 解析器,您可以简单地为每个名称使用 std::string
的 find()
:
int main()
{
std::string s("<Information name = \"Identify\" id = \"IdentifyButton\" type = \"button\" / >");
std::string names[] = { "name = \"" , "id = \"" , "type = \"" };
std::string::size_type posStart(0), posEnd(0);
for (auto& n : names)
{
posStart = s.find(n, posEnd) + n.length();
posEnd = s.find("\"", posStart);
std::string part = s.substr(posStart, posEnd - posStart);
std::cout << part << std::endl;
posEnd++;
}
}
根据您的容忍度添加错误检查 :)
您可以使用正则表达式提取由“=”分隔的 key-value 对,中间有可选的 space 个字符:
(\S+?)\s*=\s*([^ />]+)
[^ />]+
捕获由 space、/ 和 > 以外的字符组成的值。这将捕获带引号或不带引号的值。
然后将 std::regex_iterator, a read-only forward iterator, that will call std::regex_search() 与正则表达式一起使用。这是一个例子:
#include <string>
#include <regex>
#include <iostream>
using namespace std::string_literals;
int main()
{
std::string mystring = R"(<Information name="Identify" id="IdentifyButton" type="button" id=1/>)"s;
std::regex reg(R"((\S+?)\s*=\s*([^ />]+))");
auto start = std::sregex_iterator(mystring.begin(), mystring.end(), reg);
auto end = std::sregex_iterator{};
for (std::sregex_iterator it = start; it != end; ++it)
{
std::smatch mat = *it;
auto key = mat[1].str();
auto value = mat[2].str();
std::cout << key << "_part=" << value << std::endl;
}
}
输出:
name_part="Identify"
id_part="IdentifyButton"
type_part="button"
id_part=1
这是 Demo。至少需要 C++11。
我原来的字符串是这样的<Information name="Identify" id="IdentifyButton" type="button"/>
如何从这个字符串中提取 3 个子字符串 string name_part = Identify
、string id_part="IdentifyButton"
、type_part="button"
假设您不想使用 third-party XML 解析器,您可以简单地为每个名称使用 std::string
的 find()
:
int main()
{
std::string s("<Information name = \"Identify\" id = \"IdentifyButton\" type = \"button\" / >");
std::string names[] = { "name = \"" , "id = \"" , "type = \"" };
std::string::size_type posStart(0), posEnd(0);
for (auto& n : names)
{
posStart = s.find(n, posEnd) + n.length();
posEnd = s.find("\"", posStart);
std::string part = s.substr(posStart, posEnd - posStart);
std::cout << part << std::endl;
posEnd++;
}
}
根据您的容忍度添加错误检查 :)
您可以使用正则表达式提取由“=”分隔的 key-value 对,中间有可选的 space 个字符:
(\S+?)\s*=\s*([^ />]+)
[^ />]+
捕获由 space、/ 和 > 以外的字符组成的值。这将捕获带引号或不带引号的值。
然后将 std::regex_iterator, a read-only forward iterator, that will call std::regex_search() 与正则表达式一起使用。这是一个例子:
#include <string>
#include <regex>
#include <iostream>
using namespace std::string_literals;
int main()
{
std::string mystring = R"(<Information name="Identify" id="IdentifyButton" type="button" id=1/>)"s;
std::regex reg(R"((\S+?)\s*=\s*([^ />]+))");
auto start = std::sregex_iterator(mystring.begin(), mystring.end(), reg);
auto end = std::sregex_iterator{};
for (std::sregex_iterator it = start; it != end; ++it)
{
std::smatch mat = *it;
auto key = mat[1].str();
auto value = mat[2].str();
std::cout << key << "_part=" << value << std::endl;
}
}
输出:
name_part="Identify"
id_part="IdentifyButton"
type_part="button"
id_part=1
这是 Demo。至少需要 C++11。