sauro
sauro

Reputation: 62

Can't use regular expression with .*

I've been trying to use regular expressions (<regex.h>) in a C project I am developing.

According to regex101 the regex it is well written and identifies what I'm trying to identify but it doesn't work when I try to run it in C.

#include <stdio.h>
#include <regex.h>

int main() {
    char pattern[] = "#include.*";
    char line[] = "#include <stdio.h>";
    
    regex_t string;
    int regex_return = -1;
    
    regex_return = regcomp(&string, line, 0);
    regex_return += regexec(&string, pattern, 0, NULL, 0);
    
    printf("%d", regex_return);

    return 0;
}

This is a sample code I wrote to test the expression when I found out it didn't work.

Upvotes: 1

Views: 44

Answers (1)

sauro
sauro

Reputation: 62

line and pattern are swapped.

regcomp takes the pattern and regexec takes the string to check.

Upvotes: 1

Related Questions