![](/img/trans.png)
[英]how do i extract multiple substrings from a single string separated by space?
[英]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"
假设您不想使用第三方 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++;
}
}
根据您的容忍度添加错误检查:)
您可以使用正则表达式来提取由“=”分隔的键值对,中间有可选的空格字符:
(\S+?)\s*=\s*([^ />]+)
[^ />]+
捕获由空格、/ 和 > 以外的字符组成的值。 这将捕获带引号或不带引号的值。
然后使用std::regex_iterator ,一个只读的前向迭代器,它将使用正则表达式调用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;
}
}
Output:
name_part="Identify"
id_part="IdentifyButton"
type_part="button"
id_part=1
这是一个演示。 至少需要 C++11。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.