【问题标题】:search String in string在字符串中搜索字符串
【发布时间】:2016-11-30 08:15:11
【问题描述】:

我有:"ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei" 我想在"<div>"之后和"/$$/"之前获得链然后:"TOTO"

int main (int argc, char* argv[])
    {
    char IN[1000];
    char OUT[1000];
    strcpy(IN,"ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei");

    ...

    printf("%s\n",OUT);
    }

最好的问候

【问题讨论】:

标签: c string


【解决方案1】:

您有很多方法可以做到这一点,包括:

  • Regex :使用regex.h,起初这些有点难以使用,尤其是如果您以前从未操作过正则表达式。你可以在互联网上找到很多例子,StackOverflow too
  • strstr() :此函数返回一个指针,该指针指向第一个字符串参数中第一个出现的第二个字符串参数。所以,你可以试试这个方法:

    char result[32] = { 0 };
    char *str = "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei";
    
    // Find the position of the differents markers ("<div>" and "/$$/")
    char *marker1 = strstr(str, "<div>");
    
    // Check if the pattern was found
    if (! marker1) {
        printf("Could not find string \"<div>\"\n");
        return;
    }
    
    // Place marker1 to the beginning of the text to copy           
    marker1 += strlen("<div>");          
    
    // Find the second marker "/$$/"
    char *marker2 = strstr(marker1, "/$$/");
    
    // Check if the pattern was found
    if (! marker2) {
        printf("Could not find string \"/$$/\"\n");
        return;
    }
    
    // Compute the len of the text to copy
    int len = marker2 - marker1;
    
    // Copy the text to the result buffer, but take care of the buffer overflow
    strncpy(result, marker1, len < 32 ? len : 32);
    

这是一个快速示例,可以改进,但主要思想在这里。另外,请注意缓冲区长度。

【讨论】:

  • @chux : 你说得对,代码写得很快,我用你的 cmets 改进了它!
  • @chux :没错,我从您之前的评论中忘记了这一点,现在应该会更好。谢谢!
【解决方案2】:

一个简单的方法可以如下所示

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

int main( void ) 
{
    char in[] = "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei";
    char *out = NULL;
    const char start[] = "<div>";
    const char end[]   = "/$$/";

    char *p = strstr( in, start );

    if ( p )
    {
        p += sizeof( start ) - 1;
        char *q = strstr( p, end );

        if ( q )
        {
            size_t n = q - p;

            out = malloc( n + 1 );
            if ( out )
            {               
                strncpy( out, p, n );
                out[n] = '\0';
            }
        }
    }

    if ( out )
    {
        puts( out );
    }

    free( out );

    return 0;
}

程序输出是

TOTO

【讨论】:

    【解决方案3】:

    使用strstr()@pm100

    为了不给你所有的代码,使用下面的大纲。 OP 仍然需要确定 4 个... 部分。

    int main(int argc, char* argv[]) {
      char IN[1000];
      //char OUT[1000];
      strcpy(IN, "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei");
      const char *pattern1 = "<div>";
      const char *pattern2 = "/$$/";
      char *start = strstr(IN, pattern1);
      if (start) {
    
        // Increase `start` by the length of `pattern1`
        start += ...;
    
        // Search again, this time for pattern2.  From where should searching start?
        char *end = ...;
    
        if (end) {
    
          // print from start.  Only need to print some of the characters
          int length = ...
          printf("%.*s\n", ...);
    
          return 0;
        }
      }
      puts("Fail");
      return -1;
    }
    

    【讨论】:

      【解决方案4】:

      我用过这样的东西。不过也不是特别安全。因此,如果您知道 div 和 /$$/ 肯定会在字符串中,那就没问题了。为了更安全,请多阅读 strstr!

           char IN[1000];
           char* OUT;
           char* OUT2;
           int found, i;
           strcpy(IN,"ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei");
           OUT=strstr(IN,"<div>");
           OUT2=strstr(OUT,"/$$/");
           OUT2[0] = '\0';
           OUT= OUT +5;
           printf("%s\n",OUT);
      

      【讨论】:

      • 我也可以说,这也会编辑 str 的内容。如果您想将“IN”保留为未编辑,则需要进行编辑。我认为@chux 将您带到了上面的这条路线。
      • 为什么是幻数5?还要考虑如果结束模式与开始模式的结束部分匹配会发生什么。此代码不会在"&lt;div&gt;" 之后开始搜索,而是在"&lt;div&gt;" 开始搜索。
      • 5 只是跳过 5 个字符长的 '
        '。如果你想在“'
        '之后”开始搜索,那么你可以简单地将'OUT+5'放在第二个'strstr'中。我假设特定模式在这里很重要。这意味着它们只是这两种特定的模式。但你说得对,第二个 strstr 中的 'OUT+5' 是有道理的。
      猜你喜欢
      • 2019-10-06
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多