使用通配符检查文件名搜索模式中的冲突

时间:2015-11-30 23:15:10

标签: c# algorithm filenames wildcard collision

我需要比较文件系统通配符表达式,看看它们的结果是否会重叠,只需检查/比较表达式。

为了举例,我们正在构建一个实用程序,它可以根据文件系统通配符表达式将文件从一个(或多个位置)排序到单独的文件夹中。例如:* .txt进入文件夹a,* .doc进入文件夹b,依此类推。我们支持的通配符是*和?

我希望能够通过分析通配符表达式确定它们是否会发生冲突/重叠。

例如,如果我有以下表达式:

*.x.y
*.y

它们会发生冲突(重叠),因为第二个表达式* .y会包含* .x.y结果。 (例如A.x.y将匹配两个表达式)

我正在通过使用所有表达式构建树结构来接近这一点,认为如果表达式冲突,构建树的行为将失败。

For example:
*.x
a.b
a.c
b.d

might create a tree like 

         +-*-.-x
         |
start +--+
         |     +-b
         |     |
         +-a-.-+-c
         |
         |
         +-b-.-d

如果我尝试添加模式b.x,那么树会在* .x路径后成功,从而说明模式已经存在。

我是朝着正确的方向前进吗?或者是否有一种已知的攻击方法?

3 个答案:

答案 0 :(得分:11)

要检查两个通配符模式是否可以匹配相同的文件名,可以将此问题视为创建字符对之间的比较网格,然后检查是否存在对角线路径。下图显示了如何检查通配符模式ab?.c??a*bc.*是否存在冲突:

wildcard conflict animation

当找到两个相同文字字符之间的匹配时,您将对角移动到下一个要检查的字符。 (用绿色箭头表示)

当遇到文字字符和单字符通配符?时,有两种可能性:通配符匹配字符(对角移动),或通配符匹配空白空间,并跳过它。 (用紫色箭头表示)

当遇到多角色外卡*时,需要考虑三种可能性:外卡与另一个角色之前的空格匹配,外卡与另一个角色匹配,或野外卡匹配多个字符。 (用蓝色箭头表示)

wildcard conflict comparisons

代码示例1(迭代)

下面是一个简单的javascript实现,它在对角线上迭代网格,标记可以从当前单元格到达的单元格,然后检查右下角单元格是否可达。运行代码段以查看一些示例。 (更新:从上到下从左到右将做得很好,而不是对角线)

function wildConflict(wild1, wild2) {
    var grid = [[true]], width = wild1.length, height = wild2.length;
    for (var x = 1; x <= width; x++) grid[x] = [];
    for (var y = 0; y < height; y++) {
        for (var x = 0; x < width; x++) {
            if (grid[x][y]) {
                var a = wild1.charAt(x), b = wild2.charAt(y);
                if (a == '*' || b == '*' || a == '?' || b == '?' || a == b) grid[x + 1][y + 1] = true;
                if (a == '*' || b == '*' || a == '?') grid[x + 1][y] = true;
                if (a == '*' || b == '*' || b == '?') grid[x][y + 1] = true;
            }
        }
    }
    return grid[width][height] == true;
}

var a = ["a", "a", "a*", "abc", "a*", "*.x.y", "*.x.y", "a*b*", "a*bc.*", "a?c.de"];
var b = ["a", "b", "abc", "a?", "*b", "*.y", "*.x", "a*c*", "ab?.c??", "ac.d??"];
for (var i in a) document.write("&quot;" + a[i] + "&quot; &harr; &quot;" + b[i] + "&quot; &rarr; " + wildConflict(a[i], b[i]) + "<BR>");

代码示例2(递归)

简单的递归实现具有可能多次检查某些字符对的缺点。它不需要2D数组,但递归显然也使用了内存。

请注意,当遇到多字符通配符*时,算法仅使用两种可能性进行递归:跳过一个字符,或跳过另一个字符;当将通配符与下一个字符进行比较时,在下一步中处理跳过两个字符(即,通配符恰好匹配一个字符)。

function wildConflict(wild1, wild2) {
    var w1 = wild1.split(''), w2 = wild2.split('');
    return conflict(0, 0);

    function conflict(p1, p2) {
        if (p1 == w1.length || p2 == w2.length) {
            if ((p1 == w1.length && p2 == w2.length)
            || (p1 == w1.length - 1 && (w1[p1] == '*' || w1[p1] == '?'))
            || (p2 == w2.length - 1 && (w2[p2] == '*' || w2[p2] == '?'))) {
                return true;
            }
            else return false;                            // premature end
        }
        else if (w1[p1] == '*' || w2[p2] == '*' || (w1[p1] == '?' && w2[p2] == '?')) {
            return conflict(p1 + 1, p2) || conflict(p1, p2 + 1);
        }
        else if (w1[p1] == '?') {
            return conflict(p1 + 1, p2) || conflict(p1 + 1, p2 + 1);
        }
        else if (w2[p2] == '?') {
            return conflict(p1, p2 + 1) || conflict(p1 + 1, p2 + 1);
        }
        else if (w1[p1] == w2[p2]) {
            return conflict(p1 + 1, p2 + 1);
        }
        else return false;                               // unequal literals
    }
}

var x = ["a", "a", "a*", "abc", "a*", "*.x.y", "*.x.y", "a*b*", "a*bc.*", "a?c.de"];
var y = ["a", "b", "abc", "a?", "*b", "*.y", "*.x", "a*c*", "ab?.c??", "ac.d??"];
for (var i in x) document.write("&quot;" + x[i] + "&quot; &harr; &quot;" + y[i] + "&quot; &rarr; " + wildConflict(x[i], y[i]) + "<BR>");

答案 1 :(得分:4)

将每个通配符表达式转换为与其匹配的有限自动机。

计算有限自动机的交集。

使用动态编程来查看交叉点是否匹配。

如果您不认识这些概念,请参阅Algorithm for exclusion of numbers几年前我的尝试解释它。 (此时用于计算与正则表达式集合匹配的内容,但原则相同。)

答案 2 :(得分:1)

我认为您可以将模式转换为正则表达式,然后看看它们是否相互匹配?这里的解决方案基于the rules for Directory.GetFiles on MSDN - 我觉得 SOMETHING 错了,但我不确定是什么。

这是一个基本的实现

    private bool Equivalent(string patternOne, string patternTwo)
    {
        // convert both patterns to regexes based on rules for Directory.GetFiles
        var expressionOne = FilePatternToRegex(patternOne);
        var expressionTwo = FilePatternToRegex(patternTwo);

        // if either regex matches the opposite pattern, we've got a conflict
        return expressionTwo.IsMatch(patternOne) || expressionOne.IsMatch(patternTwo);
    }

    Regex FilePatternToRegex(string pattern)
    {
        // separate extension and filename
        var extension = Path.GetExtension(pattern);
        var filename = Path.GetFileNameWithoutExtension(pattern);

        // escape filename
        filename = EscapeFilePattern(filename);

        // 3 character extensions are a special case -- should be greedy eg xls matches xlsx
        // extension.Length == 4 bc its dot AND 3 characters
        if (extension.Length == 4 && !extension.Contains("*") && !extension.Contains("?"))
        {
            extension = extension + ".*";
        }
        else
        {
            // all other extension lengths just get escaped like normal regexes
            extension = EscapeFilePattern(extension);
        }

        // our final pattern should also only match at the string start/end
        var finalPattern = "\\A" + filename + extension + "\\z";

        return new Regex(finalPattern);
    }

    string EscapeFilePattern(string pattern)
    {
        // escape star and question mark bc they are filepattern significant
        pattern = pattern.Replace("*", "%S%").Replace("?", "%Q%");

        // escape all other special regex characters
        pattern = Regex.Escape(pattern);

        // turn star and question mark into their regex equivalents
        pattern = pattern.Replace("%S%", ".+").Replace("%Q%", ".");

        return pattern;
    }

编辑:根据评论中的进一步讨论,这已被打破。使用代码示例证明它已被破坏:

        var dir = new DirectoryInfo(Environment.CurrentDirectory).CreateSubdirectory(Guid.NewGuid().ToString());
        var path = Path.Combine(dir.FullName, "abc");

        File.WriteAllText(path, "*");

        // verify both patterns match our file
        Assert.AreEqual(path, dir.GetFiles("a*c*")[0].FullName);
        Assert.AreEqual(path, dir.GetFiles("a*b*")[0].FullName);

        // current regex based solution thinks they are NOT equivalent 
        // when they are
        Assert.IsFalse(Equivalent("a*c*", "a*b*"));
相关问题