解析/匹配C中出现的字符串

解析/匹配C中出现的字符串,c,regex,strstr,C,Regex,Strstr,我有以下字符串: const char *str = "\"This is just some random text\" 130 28194 \"Some other string\" \"String 3\"" 我想得到整数28194,当然整数是不同的,所以我不能做strstr20194 所以我想知道怎样才能得到那部分绳子 我在考虑使用include,我已经有了一个匹配regexp的过程,但不确定使用POSIX风格的符号时C中的regexp会是什么样子。[:alpha::+[:digit:

我有以下字符串:

const char *str = "\"This is just some random text\" 130 28194 \"Some other string\" \"String 3\""
我想得到整数28194,当然整数是不同的,所以我不能做strstr20194

所以我想知道怎样才能得到那部分绳子

我在考虑使用include,我已经有了一个匹配regexp的过程,但不确定使用POSIX风格的符号时C中的regexp会是什么样子。[:alpha::+[:digit:]以及性能是否会成为问题。还是使用strchr、strstr会更好


如果您想使用正则表达式,您可以使用:

const char *str = "\"This is just some random text\" 130 28194 \"Some other string\" \"String 3\"";
regex_t re;
regmatch_t matches[2];
int comp_ret = regcomp(&re, "([[:digit:]]+) \"", REG_EXTENDED);
if(comp_ret)
{
    // Error occured.  See regex.h
}
if(!regexec(&re, str, 2, matches, 0))
{
    long long result = strtoll(str + matches[1].rm_so, NULL, 10);
    printf("%lld\n", result);
}
else
{
    // Didn't match
}
regfree(&re);
你是对的,还有其他的方法


编辑:更改为使用非可选重复,并显示更多错误检查。

谢谢,这似乎做得很好。使用regexp而不是字符串解析是否会出现性能问题?我正在解析~1M行。我认为正则表达式是一种有效的方法。正则表达式不应该倒退。请注意,只需编译正则表达式一次,还可以重用matches数组。和往常一样,要确定。