#include <stdio.h>
#include <regex.h>
int match_regex(const char *pattern, const char *test_string) {
regex_t regex;
int reti;
// Compile the regex pattern
reti = regcomp(®ex, pattern, REG_EXTENDED);
if (reti) {
printf("Regex compilation failed.\n");
return 0;
}
// Match the test string
reti = regexec(®ex, test_string, 0, NULL, 0);
regfree(®ex); // Free memory
return reti == 0;
}
int main() {
const char *pattern = "htt+p";
const char *tests[] = {"http", "htttp", "httttp", "httpp", "htp"};
int test_count = sizeof(tests) / sizeof(tests[0]);
for (int i = 0; i < test_count; i++) {
printf("Test %d matches: %s\n", i + 1, match_regex(pattern, tests[i]) ? "true" : "false");
}
return 0;
}
// Matches "httpp": True or false, depending on how matches() method works
/*
run:
Test 1 matches: true
Test 2 matches: true
Test 3 matches: true
Test 4 matches: true
Test 5 matches: false
*/