This is the string I want to match
str = “hello_my_world”;
regex_t reg;
if (regcomp(®, pattern, REG_EXTENDED | REG_ICASE) != 0) {
exit (-1);
}
if (regexec(®, str, 0, NULL, 0) != 0) {
regfree(®);
/* did not match */
}
regfree(®);
}
if pattern is hello_* it returns true. but if pattern is hello_*_world it doesn’t…is that expected?
how can I match it?
You need to read up on regex syntax. The pattern
hello_*_worldwill match “hello”, followed by zero or more underscores, followed by yet an underscore, followed by “world”.What you want for a pattern is
hello_.*_world, which maches “hello_” followed by zero or more arbitrary chraracters, followed by “_world”.The pattern
hello_*matches, because your string contains a “hello” that is followed by zero or more underscores.