Reputation: 91
I need to search for files which match some pattern which is similar to the pattern which is given on the command line in glob
style using boost
.
For example:
If the input is myFiles*.c
it should match the files myFiles.c , myFiles1.c, myFiles123.c
etc..
Tried using the boost::regex_match
with the perl
mode. But I had to give the input pattern as myfiles.*.c
instead of myFiles*.c
. I can recognise myfiles*.c
and translate it to myFiles.*.c
and give it to the boost::regex_match
in perl mode. But the input pattern could be any vaild regex in the glob style as it is given for the command line utilities.
Is there any way in boost so that the pattern is interpreted in the glob style ?
I haven't used boost before.
Any help ? Thanks.
Upvotes: 5
Views: 3579
Reputation: 8332
Since wildcard characters differ from the meaning of the same characters in regex, you'll have to start by translating the pattern.
Do this by first replacing all '.' with "\.". Thereafter replace all '*' with ".*" and last all '?' with '.' (ofcourse without the single and double quotes ;).
Here's a C# function I've used before. It might give you an aidea of what i'm talking about.
private bool FNMatch(string fileName, string fileMask)
{
if (fileMask == "*.*" || fileMask == "*")
return true;
Regex mask = new Regex(
'^' +
fileMask
.Replace(@".", @"\.")
.Replace(@"*", @".*")
.Replace(@"?", @".")
+ '$',
RegexOptions.IgnoreCase);
return mask.IsMatch(fileName);
}
It doesn't handle all glob wildcards, but it will take care of the most common ones.
Hope this helps.
Upvotes: 1