Regex to scan integer and string from string in C -
i have string looks this: {opt,2}{home,4}... have scan opt , 2 string , integer. doing this:
char str[40] = "{opt,2}{home,4}"; char s[20]; int *x; sscanf(str, "{%[^,],%[0-9]}", s,&x); this segfaults. right way?
to scan text that, don't need regex, use simple scanf specifiers.
char s[16]; /* string in pair */ int n; /* integer in pair */ int len; /* length of pair */ (; sscanf(buf, "{%[^,],%d}%n", s, &n, &len) == 2; buf += len) { /* use str , n */ } the %n specifier receives number of characters read in sscanf call, , stores in matching argument (in case, len). use value can increment buf being read from, next string-int pair may read.
Comments
Post a Comment