如何解析C中引号中的单词?
我需要使用c从字符串中解析mac地址,如如何解析C中引号中的单词?,c,string,mac-address,C,String,Mac Address,我需要使用c从字符串中解析mac地址,如“mac”:“11:22:33:44:55:66” 我的代码: #include <stdio.h> #include <string.h> #include <stddef.h> int main() { char str[50] = "\"mac\" : \"11:22:33:44:55:66\""; char second_string[20]; sscanf(str, "what sho
“mac”:“11:22:33:44:55:66”
我的代码:
#include <stdio.h>
#include <string.h>
#include <stddef.h>
int main() {
char str[50] = "\"mac\" : \"11:22:33:44:55:66\"";
char second_string[20];
sscanf(str, "what should come here?", second_string);
printf("%s\n", second_string);
}
#包括
#包括
#包括
int main(){
char str[50]=“mac\:\“11:22:33:44:55:66\”;
字符第二个_字符串[20];
sscanf(str,“应该到这里来什么?”,第二个字符串);
printf(“%s\n”,第二个字符串);
}
我的输出应该是:
11:22:33:44:55:66
用户可以进行简单的模式匹配。因此,您可以执行以下操作
sscanf(str, "\"mac\" : \"%[^\"]\"", second_string);
“%[^”
格式与除结束前的字符以外的任何字符都匹配“]
对于更通用的解决方案,您可以找到分隔的
:'
(例如with),并且只解析字符串的最后一部分(在':'
之后)
为了更通用,请跳过冒号和所有空格(带有循环和),这样只剩下“\”11:22:33:44:55:66\”
进行解析。这可以使用如上所示的“%[^”
格式来完成。执行简单的模式匹配
sscanf(str, "\"mac\" : \"%[^\"]\"", second_string);
“%[^”
格式与除结束前的字符以外的任何字符都匹配“]
对于更通用的解决方案,您可以找到分隔的
:'
(例如with),并且只解析字符串的最后一部分(在':'
之后)
为了更通用,请跳过冒号和所有空格(带有循环和),这将只剩下
“\”11:22:33:44:55:66\”
进行解析。这可以使用“%[^”
格式完成,如上所示。感谢它的工作,但如果“mac”不会在所有情况下都一样需要通用solution@AravindUpdatedThanks的工作,但如果“mac”在所有情况下都不相同,则需要通用solution@Aravind更新