guys how i can add tags to scan like [G=Numer] / [S=Number]
guys how i can add tags to scan like [G=Numer] / [S=Number]
Help ?
If you are using Visual Studio 2012 the following will work (otherwise no support)
How to use:Code:#include <regex> #include <string> int getTagNumber(const char *tag, const char *text) { // \[tag=(\d+)\] std::string pattern = "\\["; pattern.append(tag).append("=(\\d+)\\]"); std::cmatch match; std::regex r(pattern); if (std::regex_search(text, match, r)) return atoi(match[1].first); return -1; }
If the tag does not exists it will return -1.Code:int value = getTagNumber("G", "My room name [G=31416]"); // value = 31416 value = getTagNumber("S", "My room name [G=31416][S=123]"); //value = 123
grandao i'm using visual studio 2003 it's will be work ?
No it won't work. For VS2003 or any other version use this one:
Code:int getTagNumber(const char *tag, const char *text) { char buf[20]; int i = strlen(tag); if (i > 17) return -1; sprintf(buf, "[%s=", tag); const char *pos = strstr(text, buf); if (!pos) return -1; char *end; double value = strtod(pos + i + 2, &end); if (*end == ']' && end != pos + i + 2) return value; return -1; }
Last edited by grandao; 16-08-15 at 05:11 AM. Reason: fix code