I need to extract the text between 2 string patterns in c.
Example:
aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa
PATTERN1=<BBBB>
PATTERN2=</BBBB>
Thanks.
I need to extract the text between 2 string patterns in c.
Example:
aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa
PATTERN1=<BBBB>
PATTERN2=</BBBB>
Thanks.
Just use
strstr()
.First once to find the start marker, then call it again with a pointer to the first character after the start marker, to find the end marker:
Please test the above for off-by-ones, I wrote it pretty quickly. return NULL; }
This will maybe not be optimal in performance, but very very simple to both implement, get right, read and understand.
Note: if you can't modify the original string, copy it to a buffer you can play with.
Alternate version:
ps: I haven't tested any of the code
Here is an alive example of how to do this
The output is