Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,286 questions

52,312 answers

573 users

How to find all double quote substrings in a string with C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>
#include <regex.h>

int main() {
    char str[] = "This is a string with \"double-quoted substring1\", and \"double-quoted substring2\" inside.";
    regex_t regex;
    regmatch_t matches[2];
    
    // Regular expression pattern to match substrings within double quotes
    char pattern[] = "\"([^\"]*)\"";
    
    // Compile regex
    if (regcomp(&regex, pattern, REG_EXTENDED) != 0) {
        fprintf(stderr, "Error compiling regex\n");
        return 1;
    }

    // Allocate memory for substring storage
    char extracted[128];
    
    // Searching for matches
    const char *searchPtr = str;
    while (regexec(&regex, searchPtr, 2, matches, 0) == 0) {
        int len = matches[1].rm_eo - matches[1].rm_so;
        strncpy(extracted, searchPtr + matches[1].rm_so, len);
        extracted[len] = '\0';  // Null-terminate the substring
        printf("%s\n", extracted);
        
        searchPtr += matches[0].rm_eo; // Move search pointer forward
    }

    // Free regex resources
    regfree(&regex);
    
    return 0;
}

  
/*
run:
  
double-quoted substring1
double-quoted substring2
  
*/

 



answered May 13, 2025 by avibootz
0 votes
#include <stdio.h>
#include <string.h> // strncpy
#include <regex.h>

// Function to extract and print substrings enclosed in double quotes
void extractSubstrings(const char *str) {
    regex_t regex;
    regmatch_t matches[2];

    // Regular expression pattern to match substrings within double quotes
    char pattern[] = "\"([^\"]*)\"";

    // Compile regex
    if (regcomp(&regex, pattern, REG_EXTENDED) != 0) {
        fprintf(stderr, "Error compiling regex\n");
        return;
    }

    // Allocate memory for substring storage
    char extracted[100];
    
    // Searching for matches
    const char *searchPtr = str;
    while (regexec(&regex, searchPtr, 2, matches, 0) == 0) {
        int len = matches[1].rm_eo - matches[1].rm_so;
        strncpy(extracted, searchPtr + matches[1].rm_so, len);
        extracted[len] = '\0';  // Null-terminate the substring
        printf("%s\n", extracted);

        searchPtr += matches[0].rm_eo; // Move search pointer forward
    }

    // Free regex resources
    regfree(&regex);
}

int main() {
    char str[] = "This is a string with \"double-quoted substring1\", and \"double-quoted substring2\" inside.";

    // Call the function to extract and print substrings
    extractSubstrings(str);

    return 0;
}


  
/*
run:
  
double-quoted substring1
double-quoted substring2
  
*/

 



answered May 13, 2025 by avibootz
...