#include #include #include // http://www.cplusplus.com/reference/cstdio/scanf/ // POSIX regex, http://stackoverflow.com/questions/1085083/regular-expressions-in-c-examples // GNU, http://www.gnu.org/software/libc/manual/html_node/Regular-Expressions.html // PCRE - Perl Compatible Regular Expressions, http://www.pcre.org/ // Super Light Regular Expression library, http://slre.sourceforge.net/ int main() { unsigned int uivalue; int i, ivalue, x, y; double dvalue; char c, buf[100]="zz", buf1[100], buf2[100]; // skip to the end of the line strcpy(buf, "1234 pqewiurqweir adsfads adskmcxvnz\nsecond line"); sscanf(buf, "%d%*[^\n]\n%s", &ivalue, buf1); printf("1: [%d][%s]\n", ivalue, buf1); strcpy(buf, " 123 /* hello 976d/asfhvk */ 456 "); sscanf(buf, "%d /* %*[^*]*/%d", &x, &y); // do not accept /* * */ printf("2: x=[%d] y=[%d]\n", x, y); // equivalent to %c, equivalent to space (skip all white spaces) strcpy(buf, " a"); sscanf(buf, " %[^ \t\n]", &c); printf("3: c=[%c]\n", c); sscanf(buf, "%*[ \t\n]%[^ \t\n]", &c); printf("3: c=[%c]\n", c); // equivalent to %s strcpy(buf, " hello123?-& world\n again\n"); sscanf(buf, " %[^ \t\n]", buf1); printf("4: buf1=[%s]\n", buf1); sscanf(buf, "%*[ \t\n]%[^ \t\n]", buf1); printf("5: buf1=[%s]\n", buf1); // equivalent to gets() sscanf(buf, "%[^\n]\n%s", buf1, buf2); printf("6: buf1=[%s] buf2=[%s]\n", buf1, buf2); // equivalent to fgets() sscanf(buf, "%[^\n]\n%s", buf1, buf2); strcat(buf1, "\n"); printf("7: buf1=[%s] buf2=[%s]\n", buf1, buf2); // equivalent to %d note that - and + is not restricted as the first char and occurs once strcpy(buf, " -1234 "); sscanf(buf, " %[-+0-9]", buf1); ivalue = atoi(buf1); printf("8: buf1=[%s] ivalue = %d\n", buf1, ivalue); // " %1[-+1-9]%[0-9]", buf1, &buf1[1] strcpy(buf, " +1234 "); sscanf(buf, "%*[ \t\n]%[-+0-9]", buf1); ivalue = atoi(buf1); printf("9: buf1=[%s] ivalue = %d\n", buf1, ivalue); // equivalent to %o strcpy(buf, " 012345670888 "); sscanf(buf, "%*[ \t\n]%[0-7]", buf1); for (uivalue=i=0; i='A' ? (buf1[i]>='a'? buf1[i]-'a' : buf1[i]-'A')+10 : buf1[i]-'0'); printf("11: buf1=[%s] uivalue = %d (%x)\n", buf1, uivalue, uivalue); // binary string strcpy(buf, " 01101011001 "); sscanf(buf, "%*[ \t\n]%[01]", buf1); for (uivalue=i=0; i=start) { strncpy(buf1, &buf[start], end-start); buf1[end-start] = 0; } printf("20b: start=%d end=%d buf1=[%s] (%d) failed\n", start, end, buf1, i); char *startptr, *endptr; startptr = strchr(buf, '('); endptr = strchr(startptr, ')'); for (i=0; i