如何基于标记分隔字符串
How to separate strings based on tokens
#include <iostream>
using namespace std;
void
main()
{
string target_str = "1.2.3.4:3333 servertype=simics,arch=x86";
string host_str;
string port_str;
string type_str;
string arch_str;
host_str = target_str.substr(0, target_str.find_first_of(':'));
port_str = target_str.substr(target_str.find_first_of(':')+1);
type_str = target_str.substr(target_str.find_first_of(':'));
arch_str = target_str.substr(target_str.find_first_of(':'));
}
完成后,我想要以下值:
host_str = 1.2.3.4,
port_str = 3333,
type_str = simics
arch_str = x86.
是的,正则表达式有效:
std::string target_str = "1.2.3.4:3333 servertype=simics,arch=x86";
string host_str;
string port_str;
string type_str;
string arch_str;
regex expr("([\w.]+):(\d+)\s+servertype\s*=\s*(simics|openocd)(?:\s*,\s*| )arch=(x86|x64)");
smatch match;
if (regex_search(target_str, match, expr))
{
cout << "host: " << match[1].str() << endl;
cout << "port: " << match[2].str() << endl;
cout << "type: " << match[3].str() << endl;
cout << "arch: " << match[4].str() << endl;
}
但不幸的是,这个程序必须在 Windows 和 Lunix 上编译,因此我只能使用 std 字符串
这是一个代码,用于标记std::string
给出的输入并在std::vector<string>
中输出标记。可以指定多个分隔符(在std::string
中(。
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
std::vector<std::string>
split(const std::string& str, const std::string& delim){
std::vector<std::string> result;
if (str.empty())
throw std::runtime_error("Can not tokenize an empty string!");
std::string::const_iterator begin, str_it;
begin = str_it = str.begin();
do {
while (delim.find(*str_it) == std::string::npos && str_it != str.end())
str_it++; // find the position of the first delimiter in str
std::string token = std::string(begin, str_it); // grab the token
if (!token.empty()) // empty token only when str starts with a delimiter
result.push_back(token); // push the token into a vector<string>
while (delim.find(*str_it) != std::string::npos && str_it != str.end())
str_it++; // ignore the additional consecutive delimiters
begin = str_it; // process the remaining tokens
} while (str_it != str.end());
return result;
}
int main() {
std::string test_string = ".this is.a.../.simple;;test;;;END";
std::string delim = "; ./"; // string containing the delimiters
std::vector<std::string> tokens = split(test_string, delim);
for (std::vector<std::string>::const_iterator it = tokens.begin();
it != tokens.end(); it++)
std::cout << *it << std::endl;
}
我相信你想要的是strtok((,如下面的评论中所述。 一定要做你的研究,因为它可能是一个松鼠的功能,如果你不知道你在做什么,你可能会破坏东西。 它将完全按照您的要求。 它可以需要一个令牌列表来分解字符串,然后一次将它们提供一个。
#include <iostream>
#include <string>
using namespace std;
void
main()
{
std::string target_str = "1.2.3.4:3333 servertype=simics, arch=x86";
string host_str;
string port_str;
string type_str;
string arch_str;
char *token;
char *myString = new char [target_str.length()+1];
strcpy (myString, target_str.c_str());
token = strtok(myString, ":= ");
host_str.assign(token, strlen(token));
token = strtok(NULL, ":= ");
port_str.assign(token, strlen(token));
token = strtok(NULL, ":= ");
token = strtok(NULL, ":= ");
type_str.assign(token, strlen(token));
token = strtok(NULL, ":= ");
token = strtok(NULL, ":= ");
arch_str.assign(token, strlen(token));
cout << "host: " << host_str;
cout << " port: " << port_str;
cout << " type: " << type_str;
cout << " arch: " << arch_str;
}
主机:1.2.3.4 端口:3333 类型:模拟,拱形:x86
感谢所有这些工作!!
相关文章:
- 如何分隔字符串并将标记传递给方法
- C++ 用空格和逗号分隔字符串
- 如何将整数值解析为逗号分隔字符串中的单独 int 变量?
- 如何分隔字符串并将每个部分存储在不同的向量中
- 如何按特定分隔符分隔字符串
- 逗号分隔字符串的总和,例如 O(n) 中的 4,1,3
- 如何使用符号分隔字符串而不是空格的输入字符串流
- C++中将不同类型的变量列表连接到逗号分隔字符串的最快方法是什么?
- C++ sscanf 空格分隔字符串
- 在C++中分隔字符串并存储到数组中的最佳方法
- 使用 find_first_of() 和 substr() 分隔字符串,但我得到的输出不正确
- 正在分析逗号分隔字符串中的整数
- 分析分隔字符串
- 用空格分隔字符串的最简单方法
- 分隔字符串的最佳ASCII字符是什么
- C++用空格和标点符号分隔字符串
- c#中分隔字符串中的数字和单位
- 如何基于标记分隔字符串
- c++使用wstring拆分unicode分隔字符串
- 如何在C++中用逗号分隔字符串