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的阿拉伯數字時會停止匹配。