#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
void test1()//sscanf匹配整个字符串
{
const char *str = "asdasdadsad";
char buf[1024] = { 0 };
sscanf(str, "%s", buf);
printf("%s\n", buf);
}
void test2()
{
//sscnaf匹配字符串的时候,遇到空格 \t(tab键)就会停止匹配
const char *str = "hello world!";
char buf[1024] = { 0 };
sscanf(str, "%s", buf);
printf("buf:%s\n", buf);//输出hello
}
void test3() //利用[ ]匹配取出字符串或数字
{
const char *str = "avdd1234Adfsdfs";
char buf[1024] = { 0 };
sscanf(str, "%[a-z]", buf);
printf("%s\n", buf); //输出avdd
memset(buf, 0, 1024);
sscanf(str, "%[a-z0-9]", buf);//匹配所有第一个开始是a-z0-9的字符串
printf("%s\n", buf); //输出avdd1234 遇到大写就不匹配了
const char *str1 = "1123sSasds"; //可以把字符串中的数字取出存入int型变量中
char buf1[1024] = { 0 };
int num;
sscanf(str1, "%d", &num);//匹配数字 输出1123
printf("%d\n", num);
sscanf(str1, "%d%s", &num, buf);
printf("%d %s\n", num, buf);
//const char *ip = "127.0.0.1"; //localhost
//int num1, num2, num3, num4;
//sscanf(ip, "%d.%d.%d.%d", &num1, &num2, &num3, &num4);
//printf("num1:%d\n", num1); //127
//printf("num2:%d\n", num2); //0
//printf("num3:%d\n", num3); // 0
//printf("num4:%d\n", num4); // 1
}
void test4()
{
const char *str = "1QQQ234heQQQQlloworld!";
char buf[1024] = { 0 };
sscanf(str, "%[1Q]", buf); // 取出只有1Q
printf("buf:%s\n", buf);//输出1QQQ
}
//%[^a] 只要不是a就匹配 %[^a-z]
void test5()
{
const char *str = "WERu123bcd56aplk87";
char buf[1024] = { 0 };
sscanf(str, "%[^a]", buf);
printf("buf:%s\n", buf); //匹配到第一是a的字符就停止 输出WERu123bcd56
sscanf(str, "%[^a-z]", buf);//匹配到第一个是a-z的字符就停止 输出WER
printf("buf:%s\n", buf);
}
//%*s 跳过一个字符串或%*d跳过数字
void test6()
{
{
const char *str = "abcd asdasd 1234";
char buf[1024] = { 0 };
//跳过字符串 遇到空格%*s就结束了,然后匹配%s asdasd 遇到空格结束
sscanf(str, "%*s%s", buf);//输出asdasd
printf("buf:%s\n", buf);
}
{
const char *str = "abcd1234";
char buf[1024] = { 0 };
int num;
sscanf(str, "%*[a-z]%s", buf);//跳过所有a-z的字符 然后%s匹配所有字符串
printf("buf:%s\n", buf); //输出字符串1234
sscanf(str, "%*[a-z]%d", &num);
printf("%d\n", num); //输出int类型1234
}
{
const char *str = "1234abcd";
char buf[1024] = { 0 };
sscanf(str, "%*[0-9]%s", buf);
printf("buf:%s\n", buf);//输出abcd
}
{
const char *str = "hello";
char buf[1024] = { 0 };
sscanf(str, "%3s", buf);
printf("buf:%s\n", buf);//输出hel
}
}
//hello$1234567!opjhn 匹配$和!之间字符串
void test7()
{
char *str = "hello$1234567!opjhn";
char buf[1024] = { 0 };
sscanf(str, "%*[^$]$%[^!]", buf);//*先跳过所有非$的字符,然后匹配& ,取出&后面所有非!的字符
printf("%s\n", buf);//输出为1234567
int num = 0;
sscanf(str, "%*[^$]$%d", &num);
printf("%d\n", num);//输出为1234567
}
//1.已给定字符串为: helloworld@itcast.cn, 请编码实现helloworld输出和itcast.cn输出。.
void test8()
{
char *str = "helloworld@itcast.cn";
char buf1[1024] = { 0 };
char buf2[1024] = { 0 };
sscanf(str, "%[^@]@%s", buf1, buf2);
printf("%s %s\n", buf1, buf2);
}
//2.已给定字符串为:123abcd$myname@000qwe.请编码实现匹配出myname字符串,并输出.
void test9()
{
char *str = "123abcd$myname@000qwe";
char buf[1024] = { 0 };
sscanf(str, "%*[^$]$%[^@]", buf);
printf("%s\n", buf);
}
void main()
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
system("pause");
}