将已知的字符串通过格式化匹配出有效信息
1、%*s或%*d 跳过数据
2、%[width]s 读指定宽度的数据
3、%[a-z] 匹配a到z中任意字符(尽可能多的匹配)
4、%[aBc] 匹配a、B、c中一员,贪婪性
5、%[^a] 匹配非a的任意字符,贪婪性
6、%[^a-z] 表示读取除a-z以外的所有字符
测试源码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//1、%*s或%*d 跳过数据
void test01()
{
char * str = "12345abcde";
char buf[1024] = { 0 };
sscanf(str, "%*d%s", buf);
printf("%s\n", buf);
}
void test02()
{
char * str = "abcde12345"; //在中间加空格或者\t都可以实现效果
char buf[1024] = { 0 };
//sscanf(str, "%*s%s", buf);
sscanf(str, "%*[a-z]%s", buf);
printf("%s\n", buf);
}
//2、%[width]s 读指定宽度的数据
void test03()
{
char * str = "12345abcde";
char buf[1024] = { 0 };
sscanf(str, "%7s", buf);
printf("%s\n", buf);
}
//3、%[a-z] 匹配a到z中任意字符(尽可能多的匹配)
void test04()
{
char * str = "12345abcde";
char buf[1024] = { 0 };
sscanf(str, "%*d%[a-c]", buf);
printf("%s\n", buf);
}
//4、 %[aBc] 匹配a、B、c中一员,贪婪性
void test05()
{
char * str = "aabcde12345";
char buf[1024] = { 0 };
sscanf(str, "%[acBc]", buf); //在匹配过程中,只要有一个匹配失败,后续就不在进行匹配
printf("%s\n", buf);
}
//5、%[^a] 匹配非a的任意字符,贪婪性
void test06()
{
char * str = "abcde12345";
char buf[1024] = { 0 };
sscanf(str, "%[^c]", buf);
printf("%s\n", buf);
}
//6、%[^a-z] 表示读取除a-z以外的所有字符
void test07()
{
char * str = "abcde12345";
char buf[1024] = { 0 };
sscanf(str, "%[^0-9]", buf);
printf("%s\n", buf);
}
//7、截取ip
void test08()
{
char * ip = "127.0.0.1";
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
sscanf(ip, "%d.%d.%d.%d", &num1, &num2, &num3, &num4);
printf("%d\n", num1);
printf("%d\n", num2);
printf("%d\n", num3);
printf("%d\n", num4);
}
int main(){
puts("test01 start .......................................................");
test01();
puts("test01 end .......................................................");
puts ("\n");
puts("test02 start .......................................................");
test02();
puts("test02 end .......................................................");
puts("\n");
puts("test03 start .......................................................");
test03();
puts("test03 end .......................................................");
puts("\n");
puts("test04 start .......................................................");
test04();
puts("test04 end .......................................................");
puts("\n");
puts("test05 start .......................................................");
test05();
puts("test05 end .......................................................");
puts("\n");
puts("test06 start .......................................................");
test06();
puts("test06 end .......................................................");
puts("\n");
puts("test07 start .......................................................");
test07();
puts("test07 end .......................................................");
puts("\n");
puts("test08 start .......................................................");
test08();
puts("test08 end .......................................................");
puts("\n");
system("pause");
return EXIT_SUCCESS;
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/82bd8b227354fd86ca711ac92374eb13.png)