繁体   English   中英

正则表达式 - 查找哪个组与模式不匹配

[英]Regex - Find which group is not matching the pattern

我对正则表达式很陌生,我正在尝试验证文件路径以查看它是否与我的正则表达式模式匹配,它代表文件路径的正确形式。

这是正则表达式模式:

string expression = (^P:)\\([A-Z]{3})\\(\d{6})\\(Revit)\\(Model)

我在我的 C# 程序中简单地匹配它

Match match = Regex.Match(toCheck, expression);

现在这适用于正确的文件夹路径,例如P:\SYD\121174\Revit\Model

但是,如果我输入一个几乎正确的路径,例如P:\SYD\121174\ Rhino \Model (粗体差异)我希望从match object 中得到哪个组是错误的,所以我可以将它报告给用户。
我试过这样的事情:

            foreach (Group group in match.Groups)
            {
                if (!group.Success)
                {
                    failingGroup = group.Index;
                }
            }

但后来我明白了,如果字符串不匹配,那么我就不会得到任何组。

有没有办法得到哪个组不匹配?

你的正则表达式会变得超长:

(?:(^P:)|[^\\]+)\\(?:([A-Z]{3})|[^\\]+)\\(?:(\d{6})|[^\\]+)\\(?:(Revit)|[^\\]+)\\(?:(Model)|[^\\]+)

这是一个更美化的版本:

(?:(^P:)|[^\\]+)\\
(?:([A-Z]{3})|[^\\]+)\\
(?:(\d{6})|[^\\]+)\\
(?:(Revit)|[^\\]+)\\
(?:(Model)|[^\\]+)

本质上,对于每个组,我都添加了一个替代项来匹配[^\\]+ 这将匹配除斜杠以外的任何其他内容。 因此,如果路径说Revi而不是Revit ,则组 4 ( (Revit) ) 将不匹配,但不在组 ( [^\\]+ ) 中的第二个备选方案将匹配。 现在,即使一组失败,正则表达式仍会匹配,您将必须检查每个组的Success以了解路径是否是您想要的路径:

var failedGroups = match.Groups.Cast<Group>().Where(x => !x.Success).ToList();
var success = !failedGroups.Any();

我个人不喜欢这么长的正则表达式,我会像这样使用超级宽松的正则表达式:

^([^\\]+)(?:\\([^\\]+)){4}

并检查每个组并依次捕获

if (match.Groups[1].Value != "P:") {
    // Group 1 in your original regex is wrong!
}
if (!Regex.IsMatch(match.Groups[2].Captures[0].Value, "^[A-Z]{3}$")) {
    // Group 2 in your original regex is wrong
}
if (!Regex.IsMatch(match.Groups[2].Captures[1].Value, @"^\d{3}$")) {
    // Group 3 in your original regex is wrong
}
if (match.Groups[2].Captures[2].Value != "Revit") {
    // Group 4 in your original regex is wrong
}
if (match.Groups[2].Captures[3].Value != "Model") {
    // Group 5 in your original regex is wrong
}

编辑:您似乎想要允许少于 5 个组件的路径。 您可以将{4}量词设为{0,4} (现在更加宽松),并检查第 2 组是否有 4 个捕获:

^([^\\]+)(?:\\([^\\]+)){0,4}
if (match.Groups[2].Captures.Count == 4) {
    if (match.Groups[1].Value != "P:") {
        // Group 1 in your original regex is wrong!
    }
    if (!Regex.IsMatch(match.Groups[2].Captures[0].Value, "^[A-Z]{3}$")) {
        // Group 2 in your original regex is wrong
    }
    if (!Regex.IsMatch(match.Groups[2].Captures[1].Value, @"^\d{3}$")) {
        // Group 3 in your original regex is wrong
    }
    if (match.Groups[2].Captures[2].Value != "Revit") {
        // Group 4 in your original regex is wrong
    }
    if (match.Groups[2].Captures[3].Value != "Model") {
        // Group 5 in your original regex is wrong
    }
} else {
    // the path is shorter than expected
}

我熟悉正则表达式组,但最好能演示这种验证
几乎只有 C#。
在从完整的正则表达式模式中未收到匹配项后,您可以使用此 function 来检测问题。

请看代码里面的注释:

private string FindPathProblem(string path)
{
    // check path start with p:\
    if (!path.StartsWith(@"P:\"))
    {
        return "description of the problem in group1";
    }

    // issolate [A-Z]{3}
    string group2 = path.Substring(3, path.IndexOf("\\", 3
    
    // check the length is 3
    if (group2.Length != 3)
    {
        return "description of the problem in group2";
    }
    
    // check that all 3 characters are english chars
    Regex r = new Regex("[A-Z]{3}");
    if (!r.IsMatch(group2))
    {
        return "description of the problem in group2";
    }

    // isolate 6 characters (group 3)
    string group3 = path.Substring(7, path.IndexOf("\\", 7

    // check the legth is 6
    if (group3.Length != 6)
    {
        return "description of the problem in group3";
    }

    // check all characters are numbers
    int param = 0;
    var isnumeric = Int32.TryParse(group3,out param);
    if (isnumeric == false)
    {
        return "description of the problem in group3";
    }

    // check for the existance of Revit
    string group4 = path.Substring(14, 5);
    if (group4 != "Revit")
    {
        return "description of the problem in group4";
    }

    // check for the existance of Model
    string group5 = path.Substring(20, 5);
    if (group5 != "Model")
    {
        return "description of the problem in group4";
    }
    return "OK";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM