This is the C function I am having problems with:
char get_access_token(char *client_credentials)
{
regex_t regex;
int reti;
char msgbuf[100];
reti = regcomp(®ex, "\\\"access_token\\\".\\\"(.*?)\\\"", 0);
regmatch_t pmatch[1];
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
reti = regexec(®ex, client_credentials, 1, pmatch, 0);
if (!reti) {
puts("Match");
} else if (reti == REG_NOMATCH) {
puts("No match");
} else {
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
return (char) "";
}
The string that I'm trying to parse is a JSON string, I don't care about the actual structure I only care about the access token.
It should look like this:
{"access_token": "blablablabal"}
I want my function to return just "blablablabla"
The RegEx that I'm trying to use is this one:
\"access_token"."(.*?)"
but I can't find that in the variable pmatch
, I only find two numbers in that array, I don't really know what those numbers mean.
What am I doing wrong?
P.S. I'm a C noob, I'm just learning.