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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -