c++cglobwildcard-mappingfnmatch

Is there a way to apply FNM_PATHNAME flag of fnmatch only for first instance of slash?


Hi I am trying to come up with a means to perform wildcard masking using fnmatch with certain rules.

on finding first instance of slash '/' in string, it has to be matched exactly with slash ('/') in the pattern. i.e wildcard ('*') will not be able to match. However, subsequent slashes in input string can be matched using a wildcard.

E.g for desired result:

string a/b/c should be matched using / . i.e second '/' after 'b' is matched but not the first slash after 'a'.

string: a/b/c pattern: / Result: Match. Because there is a explicit '/' in pattern for first instance of '/' in str

string: a/b/c pattern: * Result: Not match. because no explicit '/' for first instance of '/' in str

I have tried the below code part using fnmatch

int match(char pat[], char str[])
{
   int rVal = fnmatch(pat, str, FNM_PATHNAME);
   return rVal;

}

-> The above code using FNM_PATHNAME flag successfully works if i need to map every '/' in string to '/' in pattern. But not if i need to limit it to only first instance.

-> Can I continue working on this using fnmatch or do i need to depend on any other posix functionality. Please help.


Solution

  • No, there's not a way to do this directly with fnmatch, but you can just split the pattern in two at the first / and then split all of the candidate strings likewise at their first /, and match the first and second parts separately.