") in C? Here is my c" /> ") in C? Here is my c" /> ") in C? Here is my c"/>

how do i check if string contains substring and anytext (e.g "hello <any text e.g meatballs>")

136 Views Asked by At

How can I check if a string contains a specific substring along with any additional text (e.g., "hello <any text e.g., meatballs>") in C? Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/ANSI-color-codes.h"

int main(int argc, char* const argv[]) {
    if (argc != 2) {
        printf(BLU "VPWL (Very Powerful Web Language) \nyou build big bundle Small\n" reset);
        return 1;
    }

    FILE *fptr;
    fptr = fopen(argv[1], "r");

    char FileString[2097152]; // 2 MB, approximately

    while (fgets(FileString, 2097152, fptr)) {
        printf("%s", FileString);
        if (strstr(FileString, "<button('') />")) {
            // Code for processing the matching line
        }
    }

    fclose(fptr);
    return 0;
}

I would like to know how to properly check if the string in FileString contains the substring <button('') /> along with any additional text in between the Quotes (not on the beginning and end). Any help or guidance would be appreciated. Thank you!

i tried %s it did not work

2

There are 2 best solutions below

2
chqrlie On BEST ANSWER

There is no standard regular expression package in C, for your purpose you can write an ancillary function that finds the 2 substrings:

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

#define BUFFER_SIZE 2097152

int find_matches(const char *str, const char *spec1, const char *spec2, size_t *offsets) {
    const char *p1 = strstr(str, spec1);
    if (p1 == NULL)
        return 0;
    const char *p2 = p1 + strlen(spec1);
    const char *p3 = strstr(p2, spec2);
    if (p3 == NULL)
        return 0;
    const char *p4 = p3 + strlen(spec2);
    offsets[0] = p1 - str;
    offsets[1] = p2 - str;
    offsets[2] = p3 - str;
    offsets[3] = p4 - str;
    return 1;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("VPWL (Very Powerful Web Language)\nyou build big bundle Small\n");
        return 1;
    }

    FILE *fptr = fopen(argv[1], "r");
    if (fptr == NULL) {
        fprintf(stderr, "cannot open %s: %s\n", argv[1], strerror(errno));
        return 1;
    }

    char FileString[BUFFER_SIZE]; // 2 MB, approximately
    int lineno = 0;
    while (fgets(FileString, sizeof FileString, fptr)) {
        const char *p = FileString;
        size_t off[4];
        lineno++;
        //printf("%s", FileString);
        while (find_matches(p, "<button('", "') />", off)) {
            // Code for processing the matching line
            printf("%s:%d: found match: %.*s\n", argv[1], lineno,
                   (int)(off[3] - off[0]), p + off[0]);
            printf("%s:%d: substring: %.*s\n", argv[1], lineno,
                   (int)(off[2] - off[1]), p + off[1]);
            p += off[3];
        }
    }

    fclose(fptr);
    return 0;
}
0
Lundin On

Two possible approaches (this is pseudo code):

  • Either build up the exact search string in advance, for example by using strcat:
char str [LARGE_ENOUGH] = "<button('";
strcat(str, key);
strcat(str, "') />");
...
char* result = strstr(input, str);
  • Or search for "<button('" with strstr and then compare the following characters:
size_t strlen_button = sizeof("<button('") - 1;
char* result = strstr(input, "<button('");
if(result == NULL)
{ /* handle error */ }

result += strlen_button ; // advance the result pointer to the end of the found string
if(strcmp(result, key)==0) 
{
  // similarly, check if "') />" is present from here
}