#include <stdio.h>
int main()
{
char str[128];
scanf( "%[^\n]", str );
printf( "%s\n", str );
return 0;
}
scanf中的正則表達式
1、定制自己的掃描集 %[abc]、%[a-z]、%[^abc]、%[^a-z],比isdigit()、isalpha()更加靈活。[]內是匹配的字符,^表示求反集。
int i;
char str[80], str2[80];
// scanf("%d%[abc]%s", &i, str, str2);
// printf("%d %s %s\n",i,str,str2);
// scanf("%[a-zA-Z0-9]", str);
// scanf("%[^abce]", str);
scanf("%[^a-z]", str);
printf("%s\n",str);
2、讀入一個地址並顯示內存地址的內容
int main(void)
{
char ch='c';
printf("%p\n", &ch); // print the address of ch.
char *p;
cout<<"Enter an address: ";
scanf("%p", &p); //input the address displayed above
printf("Value at location %p is %c\n",p,*p);
return 0;
}
3、丟棄不想要的空白符:scanf("%c %c");
4、控制字符串中的非空白符:導致scanf()讀入並丟棄輸入流中的一個匹配字符。"%d,%d";
5、壓縮輸入:在格式碼前加上*,則用戶就可以告訴scanf()讀這個域,但不把它賦予任何變量。
scanf("%c%*c, &ch); 使用此方法可以在字符處理時吃掉多余的回車。
例1:從<sip:tom@172.18.1.133>中提取tom
const char* url = "<sip:tom@172.18.1.133>";
char uri[10] = {0};
sscanf(url, "%*[^:]:%[^@]", uri);
cout << uri << endl;
例2:從iios/12DDWDFF@122中提取 12DDWDFF
const char* s = "iios/12DDWDFF@122";
char buf[20];
sscanf(s, "%*[^/]/%[^@]", buf);
cout << buf << endl;
