#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(':'));
}
On completion I want the following values:
host_str = 1.2.3.4,
port_str = 3333,
type_str = simics
arch_str = x86.
Yes regex works:
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;
}
But unfortunately this program has to compile on Windows and Lunix so hence I have to use std strings only