-1

I'm looking for a very simple way to return an array of strings that are contained between trailing and leading strings. Here's an example:

char *text = ;;;Text I want]]] Text I don't care about ;;;More Text I want]]] More text I don't care about

Calling stringBetweenString(";;;","]]]",text) should return an array (const char *myArray[2]) with the following values: "Text I want","More Text I want".

Unfortunately, I do not have access to RegEx for this application, nor external libraries. Any help would be greatly appreciated, thanks!

Alex Wulff
  • 2,039
  • 3
  • 18
  • 29
  • 2
    `strstr()` will help you find delimiters. `strncpy` will help you copy parts of the string. – myaut May 30 '16 at 21:36
  • 1
    Looks a lot like school assignment. Show us your code what you have tried so far as @Ben suggests and then we can help. – Petr May 30 '16 at 21:38
  • I've tried using answer two on this post: http://stackoverflow.com/questions/30302294/extract-string-between-two-specific-strings-in-c but I can't figure out how to get this to work with multiple instances of the delimiters. It will only return the first instance. – Alex Wulff May 30 '16 at 21:38
  • @AlexWulff `strstr()` to find start, `strstr()` to find the end from there, `end - start` gives length (incl. starting delimiter), feed that into `strcpy()`, loop from there, done – johannes May 30 '16 at 21:41
  • @AlexWuff, `strstr()` returns a position in string as `char*`. It could later used in following calls of `strstr()` as first arg. I.e. `char* p = "a;b;c", *a, *b, *c; a = strstr(p, ";"); b = strstr(a+1, ";"); c = strstr(a+1, ";");`. In this example `a` is `;b;c`, `b` is ";c" and `c` is `NULL` – myaut May 30 '16 at 21:43
  • @myaut - thanks, that is very helpful. I'll try this again and see if I can get it working. I guess I was confused about what exactly strstr() was doing. – Alex Wulff May 30 '16 at 21:46

1 Answers1

2

There is no need for a regex, as others have noted strstr will search within a string for the occurrence of a substring, returning a pointer to the beginning of the substring on success, NULL otherwise. You can use that with simple pointer arithmetic to parse the wanted text from between the substrings, e.g.:

#include <stdio.h>
#include <string.h>

#define MAXC 128

int main (void) {

    char *text = ";;;Text I want]]] Text I don't care about ;;;More "
                "Text I want]]] More text I don't care about";
    char buf[MAXC] = "", *p = text, *ep;

    while ((p = strstr (p, ";;;"))) {
        if ((ep = strstr (p, "]]]"))) {
            strncpy (buf, p + 3, ep - p - 3);
            buf[ep - p - 3] = 0;
            printf ("buf: '%s'\n", buf);
        }
        else
            break;
        p = ep;
    }

    return 0;
}

Example Use/Output

$ ./bin/splitbetween
buf: 'Text I want'
buf: 'More Text I want'
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • This solution will fail if you swap to positions of `'Text I want'` and `'More Text I want'` (or use a different platform, such as the DeathStation3000) Hint: `strncpy()` – wildplasser May 30 '16 at 22:45
  • This works awesome, thanks. So if I'm understanding this correctly, you're checking to see if there's an instance of the beginning cap still existing in the string which would imply that there's still some text left to be found. You're then using the lengths of the extracted strings from strstr to copy over the new string and add it to the buffer? – Alex Wulff May 31 '16 at 21:39
  • Yes, basically `strstr` will return a pointer to the beginning of your search string (either `;;;` or `]]]` in your case) or it returns `NULL` if not found. So I search for the first string and assign it to pointer `p`, then search for the second string and assign it to *end pointer* `ep`. Knowing I need to skip the first 3 chars (the `;;;`) I tell `strncpy` to copy from `p + 3` and then tell it to copy `ep - p - 3` bytes. (the length - `3` to account for starting at `p + 3`). You can parse anything in C with good old pointer arithmetic `:)` – David C. Rankin May 31 '16 at 22:37
  • Wow - thanks so much for the detailed answer and explanation. This was very helpful. – Alex Wulff May 31 '16 at 22:43