[英]How to extract multiple substrings from a string?
My original string looks like this <Information name="Identify" id="IdentifyButton" type="button"/>
我的原始字符串如下所示<Information name="Identify" id="IdentifyButton" type="button"/>
from this string, how do extract 3 substrings string name_part = Identify
, string id_part="IdentifyButton"
, type_part="button"
从这个字符串中,如何提取 3 个子string name_part = Identify
, string id_part="IdentifyButton"
, type_part="button"
Assuming you don't want to use third-party XML parsers, you can simply use std::string
's find()
for each of your names:假设您不想使用第三方 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++;
}
}
Add error checking per your tolerance:)根据您的容忍度添加错误检查:)
You could use a regex to extract key-value pairs separated by a '=' with optional space characters in between:您可以使用正则表达式来提取由“=”分隔的键值对,中间有可选的空格字符:
(\S+?)\s*=\s*([^ />]+)
[^ />]+
captures a value consisting of characters other than space, / and >. [^ />]+
捕获由空格、/ 和 > 以外的字符组成的值。 This will capture values with or without quotes.这将捕获带引号或不带引号的值。
Then use std::regex_iterator , a read-only forward iterator, that will call std::regex_search() with the regex.然后使用std::regex_iterator ,一个只读的前向迭代器,它将使用正则表达式调用std::regex_search() 。 Here's an example:这是一个例子:
#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: Output:
name_part="Identify"
id_part="IdentifyButton"
type_part="button"
id_part=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.