So, as I was searching for easier ways to parse tagged txt files, I came across this amazing function, sscanf from <stdio.h>, which seems to be the perfect thing I need to do it. The function seems to be able to do lots of filtering and scanning to return the only part of the string I would need. So suppose, I want to extract what is between the "[f=", and the "[/f]" from "[f=0]img=3 delay=1 goto=0 state=0 center=64,103 f_x=0 f_y=0 f_z=0[/f]" into a string . I will just use:
sscanf(line.c_str(), "[f=%[^[/f]]", &extracted);
But the problem is that it only returns: "0]img=3 delay=1 goto=0 state=0 center=64,103" as if it stopped at "f_x" because it contains an "f" which can be found inside the "[/f]". So how can I make it only stop if it finds the EXACT "[/f]" as a whole? Also, can I make it ignore any characters it finds before the "[f=" token?

Any help or suggestions for alternatives are appreciated.
Thanks in advance.
Last edited on
It sounds like you want a regular expression. Boost has one you might want to consider looking at, though I've not used it often myself.
sscanf( line.c_str(), "[f=%*[1234567890]]%[^[]", extracted )

This will extract "img=3 delay=1 goto=0 center=64,103 f_x=0 f_y=0 f_z=0", however if the string may contain another '[' between the opening [f=?] and [/f] the extraction will end at the first '[' after the opening tag. As Zhuge mentioned, using a regex implementation would be more flexible, and I would definitely recommend it if you're going to be doing more things like this in your code.
Thanks guys! That worked! And I will check this regex implementation in sometime. Thanks for pointing that out.
Topic archived. No new replies allowed.