Search code examples
cstrtokstring-parsing

string parsing with strtok_r


my string looks like this:

abcd "efgh [data\]" pqrl 12fgd]

I want to parse till ']' which is not proceeded by a backslash '\'

Can I do it with strtok_r? If not than how should I do it?


Solution

  • There is no one shot method to doing this using strtok_r. Since your delimiter is a single character you can always reconstruct the string you want by stuffing back the delimiter if the last character of a token returned by strtok_r is '\'.

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char str[] = "abcd \"efgh [data\\]\" pqrl 12fgd]";
      char *tokens[2] = {0};
      char *pToken = str;
      unsigned int i = 0;
    
      for( tokens[i] = strtok_r( str, "]", &pToken ); ++i < 2; 
        tokens[i] = strtok_r( NULL, "]", &pToken ) ) {
      }
    
      for( i = 0; i < 2; ++i ) {
        printf( "token %d = %s\n", i, tokens[i] );
      }
    
      for( i = 0; i < 2; ++i ) {
        if( tokens[i][strlen(tokens[i]) - 1] == '\\' ) {
          tokens[i][strlen(tokens[i])] = ']';
        }
      }
    
      printf( "output = %s\n", str );
    
      return 0;
    }
    

    This outputs:

    token 0 = abcd "efgh [data\
    token 1 = " pqrl 12fgd
    output = abcd "efgh [data\]" pqrl 12fgd