#include <regex> std::regex pattern("^(\\d+)\\s+used\\s+hard\\s+(\\d+)\\s+label\\s+(\\d+)"); std::cmatch match; regex_match(input_c_string, match, pattern); if (match.size() == pattern.marc_count() + 1) { mystruct.firstvar = stoi(match[1]); mystruct.secondvar = stoi(match[2]); mystruct.thirdvar = stoi(match[3]); } else { std::cerr << "string does not match" << std::endl; // Тут должна быть Ваша обработка неподходящей строки }
Something like this. http://www.cplusplus.com/reference/regex/basic_regex/
IMHO, write your parser (although it is not complicated here) - reinvent the wheel, which has long been in the STL
On pure C (checked, working) add the third number by analogy:
#include <stdio.h> #include <stdlib.h> typedef struct { int f; int s; int t; } Hard; int parse_string (const char *s, Hard *h) { int i = 0; int j; while (s[i] >= '0' && s[i] <= '9') ++i; if (i == 0 || s[i] == '\0') return 0; char *buffer = (char*) malloc(i + 1); for (j=0; j<i; j++) buffer[j] = s[j]; buffer[i] = '\0'; h->f = atoi(buffer); free(buffer); while((s[i] < '0' || s[i] > '9') && s[i] != '\0') ++i; if (s[i] == '\0') return 0; int snd_begin = i; while (s[i] >= '0' && s[i] <= '9') ++i; if (s[i] == '\0') return 0; buffer = malloc(i - snd_begin + 1); for (j=snd_begin; j<i; j++) buffer[j-snd_begin] = s[j]; buffer[i-snd_begin] = '\0'; h->s = atoi(buffer); free(buffer); // То же для третьего числа return 1; } int main(void) { const char s[] = "22 xxx yyy zzz 33 www"; Hard h; if (parse_string(s, &h)) { printf("%d %d\n", hf, hs); } else { printf("doesnt match\n"); } return 0; }