-2

I have : "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei" and I would like obtain the chain after "<div>" and before "/$$/" then : "TOTO"

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

    ...

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

Best regards

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
christophe
  • 27
  • 1
  • 5
  • 4
    strstr is probably a good starting point – pm100 Jul 26 '16 at 15:26
  • 3
    this is not a free code writing service. What have you tried, what were your problems? What have you looked at? I'm sure you can find lots of good stuff when searching for things like "C extract string from string" or so. – Marcus Müller Jul 26 '16 at 15:32
  • 3
    Possible duplicate of [check substring exists in a string in C](http://stackoverflow.com/questions/12784766/check-substring-exists-in-a-string-in-c) – mhk Jul 26 '16 at 15:35
  • 2
    This will help: http://stackoverflow.com/questions/7500892/get-index-of-substring. – Kunal Kapoor Jul 26 '16 at 15:35

4 Answers4

3

Use strstr() @pm100.

To not give you all the code, use the below outline. OP still needs to determine tthe 4 ... parts.

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;
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

I used something like this. It isn't particularly safe though. So if you know both div and /$$/ will definitely be in the string, it'll be fine. To make it safer, read up on strstr some more!

     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);
Shaun Ramsey
  • 562
  • 4
  • 14
  • I could also say, this edits the contents of the str as well. IF you want to leave "IN" as un-edited, you'll need to make edits. I think @chux was leading you to this route above. – Shaun Ramsey Jul 26 '16 at 15:55
  • Why the magic number `5`? Also consider what happens if the end pattern matches the end portion of the start pattern. This code does not start searching "after `"
    "`, but at `"
    "`.
    – chux - Reinstate Monica Jul 26 '16 at 16:05
  • 5 is simply to skip '
    ' which is 5 characters long. If you want to start searching "after '
    '" then you can simply put 'OUT+5' in the second 'strstr'. I was assuming the specfic patterns are what was important here. Meaning that they were simply these two specific patterns. But you're right in that 'OUT+5' in the second strstr makes sense.
    – Shaun Ramsey Jul 26 '16 at 17:33
1

You have many ways to do this, including :

  • Regex : using regex.h, these are a little bit hard to use at first, espacially if you never manipulate regex before. You could find many example on internet, on StackOverflow too.
  • strstr() : this function returns a pointer to the first occurence of the second string parameter inside the first string parameter. So, you could try this way :

    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);
    

This is a fast example, it could be improved, but the main idea is here. Also, take care to the buffer length.

Community
  • 1
  • 1
Quentin
  • 724
  • 7
  • 16
0

A straightforward approach can look the following way

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

The program output is

TOTO
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335