c++ - Would Rewriting It Using Regex Shorten/Beautify The Code? -
the problem little challenging because want code using std::regex
believing easier read , faster write.
but seems can code 1 way (shown below).
somehow mind not see solution using std::regex
.
how code it?
would using std::regex_search
job?
/* input: data coming in: /product/country/123456/city/7890/g.json input: url parameter format: /product/country/<id1:[0-9]+>/city/<id2:[0-9]+>/g.json output: std::vector<std::string> urlparams sample output: urlparams[0] = "123456" urlparams[1] = "7890" */ bool parseit(const char *path, const char* urlroute, std::vector<std::string> *urlparams) { const dword bufsz = 2000; char buf[bufsz]; dword dwsize = strlen(urlroute); urlparams.clear(); int j = 0; int = 0; bool = false; (i = 0; < dwsize; i++) { char c1 = path[j++]; char c2 = urlroute[i]; if (c2 == '<') { = true; while (c2 != '/') { i++; c2 = urlroute[i]; } int k = 0; memset(buf, 0, bufsz); while (c1 != '/') { buf[k++] = c1; c1 = path[j++]; } urlparams->push_back(_strdup(buf)); int b = 1; } if (c1 != c2) { return false; } if (c2 != '<') { if (c1 == c1) { } else { return false; } } } if (dwsize == && good) { return true; } return false; }
the easiest 1 i've found (might not best should work input data) is
std::string subject("/product/country/123456/city/7890/g.json"); std::regex re("/(\d+)/city/(\d+)/"); std::smatch match; std::regex_search(subject, match, re);
it matches 2 values per line. /
matches slash @ beginning/end , ()
capture. have convert string type though.
Comments
Post a Comment