//(1)取出/后的字符串 TCHAR buf[12] = { 0 }; swscanf_s(_T("a/n"), _T("%*[^/]/%s"), buf,12); TRACE(_T("%s"), buf); //(2)取出分隔字符串 TCHAR buf3[10] = { 0 }; TCHAR buf4[10] = { 0 }; swscanf_s(_T("a=b"), _T("%[^=] %*[=]%s"), buf3, 10,buf4,10); TRACE(_T("%s %s \n"), buf3,buf4); //(3)取出时间值 int a, b, c; swscanf_s(_T("2018-12-29"), _T("%d-%d-%d"), &a, &b, &c); TRACE(_T("%d-%d-%d"), a, b, c); //(4)取出大小字字符串 TCHAR buf1[12] = { 0 }; TCHAR buf2[12] = { 0 }; swscanf_s(_T("anAB"), _T("%[a-z]%[A-Z]"), buf1, 12,buf2,12); TRACE(_T("%s ,%s"), buf1,buf2); //(5)IP地址 int ip[4] = { 0 }; int ret = sscanf_s(" 192. 168.1. 213", "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); if(ret != 4)TRACE(_T("ip error")); if (ip[0] > 255 || ip[0] <= 0 || ip[1]>255 || ip[1] <= 0 || ip[2]>255 || ip[2] <= 0 || ip[3]>255 || ip[3] <= 0) { TRACE(_T("ip error")); } else { TRACE(_T("ip ok")); }
邮件地址判断,如下:
TCHAR buf1[12] = { 0 }; TCHAR buf2[12] = { 0 }; TCHAR buf3[12] = { 0 }; int ret = swscanf_s(_T("abc2018@QQ.com"), _T("%[a-zA-B0-9] %*[@] %[a-zA-Z]%*[.]%s"), buf1, 12,buf2,12,buf3,12); TRACE(_T("ret=%d: %s %s %s\n"),ret, buf1,buf2,buf3); if (ret == 3) TRACE(_T("正确的邮件地址")); else TRACE(_T("不正确的邮件地址"));
取出串口号后的数字,如下:
CString strCOM = _T("COM12"); TCHAR buff[10] = {}; swscanf_s(strCOM, _T("%*[a-zA-Z]%[0-9]"), buff, 10); TRACE(_T("%s\n"),buff);
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。