利用正则表达式:
参考:http://blog.youkuaiyun.com/yangbingzhou/article/details/51352648
#include <regex.h>
char tmpbuf[128] = {0};
char buffer[128] = {0};
char sizebuf[30] = {0};
int ret = 0, res = 0;
int size = 0;
regex_t re;
regmatch_t match[1];
memset(&re, 0, sizeof(re));
res = regcomp(&re, "[0-9]+", REG_EXTENDED);
if (res) {
sprintf(ddr_size, "Unknown");
goto out;
}
ret = regexec(&re, tmpbuf, 1, match, 0);
if (ret == 0 && match[0].rm_so >= 0)
memcpy(sizebuf, &tmpbuf[match[0].rm_so],
(match[0].rm_eo-match[0].rm_so));
size = atoi(sizebuf);
如果是格式已知的字符串, 如 "profile3", 从中提取数字3, 则可以使用函数sscanf, 如下:
char profile[20] = "profile3";
int err, profile_id;
err = sscanf(profile, "profile%d", &profile_id);
参考: https://www.cnblogs.com/lanjianhappy/p/6861728.html
例子: 从文件中提取数字<