How to find a substring between quotes in C

If I have a line such as a line that is a command

echo 'foobar' | cat

Is there a good way to get text between quotes ("foobar")? I read what can be used scanffor this in a file, is it also possible in memory?

My attempt:

  char * concat2 = concat(cmd, token);
  printf("concat:%s\n", concat2);
  int res = scanf(in, " '%[^']'", concat2);
  printf("result:%s\n", in);
+4
source share
3 answers

If your line is in this format - "echo 'foobar'|cat", sscanfit can be used -

char a[20]={0};
char *s="echo 'foobar'|cat";
if(sscanf(s,"%*[^']'%[^']'",a)==1){
   // do something with a
} 
else{
 // handle this condition 
}

%*[^']will read and discard the string until it encounters a single quote ', the second format specifier %[^']will read the string before 'and save it to a.

+3

strtok() , , (' ), , , :

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

int main(void) {
  const char* lineConst = "echo 'foobar'|cat"; // the "input string"
  char line[256];  // where we will put a copy of the input
  char *subString; // the "result"

  strcpy(line, lineConst);

  subString = strtok(line, "'"); // find the first double quote
  subString=strtok(NULL, "'");   // find the second double quote

  if(!subString)
    printf("Not found\n");
  else
    printf("the thing in between quotes is '%s'\n", subString);
  return 0;
}

:

- "foobar"


: C?

+4

. , , , string.h. , strchr , strpbrk, , strtok ..

. strpbrk . , .

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

int main (void) {

    const char *line = "'foobar'|cat";
    const char *delim = "'";        /* delimiter, single quote */
    char *p, *ep;

    if (!(p = strpbrk (line, delim))) { /* find the first quote */
        fprintf (stderr, "error: delimiter not found.\n");
        return 1;
    }
    p++;                        /* advance to next char */
    ep = strpbrk (p, delim);    /* set end pointer to next delim */

    if (!p) {   /* validate end pointer */
        fprintf (stderr, "error: matching delimiters not found.\n");
        return 1;
    }

    char substr[ep - p + 1];        /* storage for substring */
    strncpy (substr, p, ep - p);    /* copy the substring */
    substr[ep - p] = 0;             /* nul-terminate */

    printf ("\n single-quoted string : %s\n\n", substr);

    return 0;
}

/

$ ./bin/substr

 single-quoted string : foobar

Without using string.h

As mentioned above, you can also just go a couple of pointers down the line and find your quotes pairs in the same way. For completeness, here is an example of finding several quoted lines in a single line:

#include <stdio.h>

int main (void) {

    const char *line = "'foobar'|cat'mousebar'sum";
    char delim = '\'';
    char *p = (char *)line, *sp = NULL, *ep = NULL;
    size_t i = 0;

    for (; *p; p++) {                /* for each char in line */
        if (!sp && *p == delim)             /* find 1st delim */
            sp = p, sp++;                   /* set start ptr  */
        else if (!ep && *p == delim)        /* find 2nd delim */
            ep = p;                         /* set end ptr    */
        if (sp && ep) {                     /* if both set    */
            char substr[ep - sp + 1];       /* declare substr */
            for (i = 0, p = sp; p < ep; p++)/* copy to substr */
                substr[i++] = *p;
            substr[ep - sp] = 0;            /* nul-terminate  */

            printf ("single-quoted string : %s\n", substr);
            sp = ep = NULL;
        }
    }

    return 0;
}

Usage / Output Example

$ ./bin/substrp
single-quoted string : foobar
single-quoted string : mousebar

See all the answers and let us know if you have any questions.

0
source

All Articles