I'm only able to match simple patterns like: "[0-9]"
with fnmatch("[0-9]", tocheck, 0)
.
If I try something more complicated with ?
or .
or even a combination of these how do I use fnmatch
?
I saw there are some flags that can do the trick, but I don't know how to use because I'm fairly new to C.
EDIT: I saw the comment asking to give more details:
#include <stdio.h>
#include <fnmatch.h>
int main(int argc, char **argv) {
const char *patternOne = "[0-9]";
const char *patternTwo = ".?[a-z0-9]*?*[a-z0-9]";
int res = fnmatch(patternTwo, "0", 0);
printf("Result: %d\n", res);
}
If I use patternOne
the result is 0
and if I change the string to match, the result change correctly.
However if I use patternTwo
I never get the 0
result for whatever string I pass to fnmatch
.
I need to match something like this in my program. It is for an university exam, so the patterns are very intricate.
fnmatch
? – Mcripleyfnmatch
is supposed to match patterns in a string, should you be using['0'-'9']
and not[0-9]
? Is there a similarity withscanf
's%[]
specifer? – Mcripleyfnmatch()
POSIX spec: pubs.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html – Ethefnmatch()
doesn't match regular expressions. As a quick example, the fact thatpatternTwo
starts with a'.'
character means it won't match"0"
since forfnmatch()
a period matches a period (not any character like in a regex). For example, tryfnmatch(patternTwo, ".x0y1", 0)
will will show a match. – Ethe