#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字符串
答案 0 :(得分:0)
这是一个代码,用于对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;
}
答案 1 :(得分:0)
我相信你想要的是strtok(),如下面的评论所述。一定要做你的研究,因为它可能是一个简单的功能,如果你不知道你在做什么,你可以打破一些事情。它会完全按照你的意愿行事。它可以使用一个标记列表来分解字符串,然后一次一个地给它们。
答案 2 :(得分:0)
#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类型:simics,arch:x86
感谢所有这些工作!!