Reputation: 3343
I have a string containg spaces and tags like:
<note label="description">sp|P02671|FIBA_HUMAN Fibrinogen alpha chain OS=Homo sapiens GN=FGA PE=1 SV=2</note>
I want to capture only the part after the description tag and before 'OS=' and was wondering if it makes sense to do with sscanf with a custom notation (see my current work in progress at bottom of page) or if it's better to use a second strstr to parse for OS.
Thanks in advance
-- Additional info --
if ( (p_str = (char*) strstr(buffer,"\"description\">")) ) {
sscanf(p_str+14,"%[^OS]",(file+teller)->description);
}
PS: %[^OS] breaks the string at the first occurance of a 'O', i would love to know how to enter a set of characters (if that's possible).
Upvotes: 3
Views: 176
Reputation: 23707
I would rather use strstr
twice, as follow:
#include <assert.h>
#include <stdlib.h>
#include <string.h>
char *f (const char *s) {
#define START_SYM "\"description\""
#define END_SYM "OS="
char *dst = NULL;
char *start = strstr(s, START_SYM);
char *end = strstr(s, END_SYM);
if (start != NULL && end != NULL) {
ptrdiff_t diff = end - start;
assert(diff > 0);
dst = malloc(diff + 1);
if (dst != NULL) {
memcpy(dst, start + sizeof START_SYM, diff);
dst[diff] = '\0';
}
}
return dst;
}
Upvotes: 1