sscanf使用小记

sscanf介绍

在高级语言中比如Python,我们通常会用正则表达式(regex)处理复杂字符串, 比如校验手机号码格式的正确性,提取URL里面的一些关键信息。 在C语言中也有一个函数sscanf可以达到类似的功能,但是不及正则表达式灵活强大。

sscanf用法

/*
*   \param str: 被解析的字符串
*   \param format: 解析表达式
*   \param __VA_ARGS__: 可变变量 
*   \return int: -1 参数格式错误 >=0 匹配的变量个数
*/
int sscanf ( const char *str, const char *format, ...)

Example:
这是模拟的一条字符串,现在要求从中提取出hello world

int main()
{
    char STR[] = "+PUBLISH:0,/a1FgtnpfTdI/deviceA/get,12, hello world\r\n";
    char temp[64];
    int len = 0, id = 0, result = 0;

    result = sscanf(STR, "%*[^:]:%d,%*[^,],%d,%[^\r]", &id, &len, temp);
    if (result == 3) {
        printf("id: %d, length: %d, content: %s", id, len, temp);
    } else {
        printf("match failed.");
    }

    return 0;
}

运行结果为

id: 0, length: 12, content:  hello world

sscanf的使用方式看起来和scanf类似,我们可以把它看作是scanf的逆操作。 使用scanf时,我们使用格式化字符串和对应的变量,生成所需的字符串。 现在则是根据格式字符串,从一个已存在的字符串中提取出我们想要的关键内容。

解析语法: %[*][width][modifiers]type

  • % 代表变数的开始

  • _*_ 忽略变量

  • width 读取长度

  • modifier 参数的字节长度

  • type 一个字符,指定了要被读取的数据类型以及数据读取方式

现在让我们通过一些简单的例子来掌握语法的使用:

1、使用%s获取一个字符串

char str[] = "hello John!";
char Temp[128], Temp2[128];

result = sscanf(str, "%s", Temp);
printf("the content is \"%s\", result is %d.\r\n", Temp, result);

result = sscanf(str, "%s %s", Temp, Temp2);
printf("the content is \"%s\" and \"%s\", result is %d.\r\n", Temp, Temp2, result);

结果为:

the content is "hello", result is 1.                            
the content is "hello" and "John!", result is 2. 

从上面可以看到%s只匹配到了hello, 这是为什么呢? %s其实会忽略空格, 所以我们需要两个%s,才能匹配被空格分割的字符串。被%s忽略的还有\r,\t等等,需要我们多加注意。

2、忽略部分字符串

char str[] = "hello John!", Temp[128];
result = sscanf(str, "%*s%s", Temp);
printf("the content is \"%s\", result is %d.\r\n", Temp, result);

结果为:

the content is "John!", result is 1.

%*s忽略了空格前的hello, 只有后半部分的John!被匹配到。 *作用是忽略格式串匹配到的字符串。

3、获取指定长度的字符串

char str[] = "hello John!", Temp[128];
result = sscanf(str, "%2s", Temp);
printf("the content is \"%s\", result is %d.\r\n", Temp, result);

结果为:

the content is "he", result is 1.

%[width]s可以匹配指定长度的字符串, 但是仍然会被空格等字符截断。

4、匹配到指定字符为止

char str[] = "http://www.baidu.com/";
char Temp[128];

result = sscanf(str, "%*[^/]//%[^/]", Temp);
printf("the content is \"%s\", result is %d.\r\n", Temp, result);

结果为:

the content is "www.baidu.com", result is 1.

%[^/]的意思是匹配/首次出现的地方,看过第2个例子的同学,想必已经知道%*[^/]的意思就是
忽略/之前的字符串。 我们要从URL中获取www.baidu.com,很自然的想法就是以两个/为边界,直接提取关键词。

5、获取指定字符集为止的字符串

char str[] = "123northFace";
char Temp[128];

result = sscanf(str, "%[a-z1-9]", Temp);
printf("the content is \"%s\", result is %d.\r\n", Temp, result);

结果为:

the content is "123north", result is 1. 

我们可以通过类似[a-zA-Z1-9]这样的集合来匹配字符串。在本例中[a-z1-9]在遇到小写字母,1到9的阿拉伯数字时会停止匹配。

猜你喜欢

转载自www.cnblogs.com/soga238/p/10242201.html