如何在C中找到引号之间的子串

时间:2016-04-24 19:01:59

标签: c string

如果我有一个字符串,例如作为命令的字符串

  回声'foobar'|猫

有没有一种方法可以让我在引号(“foobar”)之间获取文字?我读过可以使用scanf在文件中执行此操作,是否也可以在内存中?

我的尝试:

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

3 个答案:

答案 0 :(得分:4)

使用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'

我的基础是:How to extract a substring from a string in C?

答案 1 :(得分:3)

如果您的字符串采用此格式 - "echo 'foobar'|cat",则可以使用sscanf -

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

%*[^']将读取并丢弃字符串,直到遇到单引号',第二个格式说明符%[^']将读取字符串直到'并将其存储在{{1}中}}。

答案 2 :(得分:1)

有很多方法可以解决这个问题。从沿着字符串向下走一对指针来定位分隔符,以及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

不使用string.h

如上所述,您还可以简单地沿着字符串向下走一对指针,并以这种方式找到您的引号对。为了完整起见,下面是一个在一行中查找多个带引号的字符串的示例:

#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;
}

示例使用/输出

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

查看所有答案,如果您有任何疑问,请告知我们。