C# 始终返回false的正则表达式

C# 始终返回false的正则表达式,c#,regex,C#,Regex,目标: static void Main(string[] args) { string folderPath = "C:\\"; string disallowedPattern = "^[A-Z]:\\$"; if (Regex.IsMatch(folderPath, disallowedPattern)) { Console.WriteLine("You chose a drive! Not cool!"); } else

目标:

static void Main(string[] args)
{
    string folderPath = "C:\\";
    string disallowedPattern = "^[A-Z]:\\$";

    if (Regex.IsMatch(folderPath, disallowedPattern))
    {
        Console.WriteLine("You chose a drive! Not cool!");
    }
    else
    {
        string suggestedProjectName = Regex.Replace(folderPath, "^.+\\([^\\]+)$", "$1");
        Console.WriteLine(suggestedProjectName);
    }
}
使用正则表达式仅显示路径的实际文件夹名称。比如说,

C:\Users\Harti\GitHub
应该减少到

GitHub
应该有进一步的规则来防止用户将驱动器作为文件夹输入


问题:

static void Main(string[] args)
{
    string folderPath = "C:\\";
    string disallowedPattern = "^[A-Z]:\\$";

    if (Regex.IsMatch(folderPath, disallowedPattern))
    {
        Console.WriteLine("You chose a drive! Not cool!");
    }
    else
    {
        string suggestedProjectName = Regex.Replace(folderPath, "^.+\\([^\\]+)$", "$1");
        Console.WriteLine(suggestedProjectName);
    }
}
  • if
    条件返回
    False
    ,我不知道为什么

  • 带有
    Regex.Replace(…)
    的行抛出一个ArgumentException,告诉我(粗略翻译)在分析序列
    ^.+\([^\]+)$
    时,“有一个未完成的[]子句”


  • 我的想法:

    static void Main(string[] args)
    {
        string folderPath = "C:\\";
        string disallowedPattern = "^[A-Z]:\\$";
    
        if (Regex.IsMatch(folderPath, disallowedPattern))
        {
            Console.WriteLine("You chose a drive! Not cool!");
        }
        else
        {
            string suggestedProjectName = Regex.Replace(folderPath, "^.+\\([^\\]+)$", "$1");
            Console.WriteLine(suggestedProjectName);
        }
    }
    
  • 我选择的任何编辑器都会将“C:\”标记为指定表达式的匹配项

  • [^\\\]
    中的双反斜杠可能被视为实际从右方括号中转义,因此使正则表达式无效。但是,对字符串使用
    @“…”
    会导致相同的异常


  • 你能给我指出我可能缺少的东西吗?我对VS和C#完全陌生,所以可能有一些异常情况我还不知道

    代码:

    static void Main(string[] args)
    {
        string folderPath = "C:\\";
        string disallowedPattern = "^[A-Z]:\\$";
    
        if (Regex.IsMatch(folderPath, disallowedPattern))
        {
            Console.WriteLine("You chose a drive! Not cool!");
        }
        else
        {
            string suggestedProjectName = Regex.Replace(folderPath, "^.+\\([^\\]+)$", "$1");
            Console.WriteLine(suggestedProjectName);
        }
    }
    

    也许你应该写信

    string disallowedPattern = "^[A-Z]:\\\\$";
    

    因为反斜杠必须在regexp和C字符串文本中转义。

    可能您应该编写

    string disallowedPattern = "^[A-Z]:\\\\$";
    

    既然反斜杠必须在regexp和C字符串文本中转义。

    为什么不使用正则表达式来解析出路径信息,为什么不使用?

    而不是使用正则表达式来解析出路径信息,为什么不使用?

    使用字符串文本更简单:
    @“C:\”
    @“^[A-Z]:\$”
    。否则,您必须将正则表达式更改为“^[A-Z]:\\\\$”,因为您必须为字符串转义一次反斜杠,为正则表达式引擎转义一次。这几乎是正确的。我必须使用
    @^[A-Z]:\\$
    。谢谢更简单的方法是使用字符串文字:
    @“C:\”
    @“^[A-Z]:\\\$”
    。否则,您必须将正则表达式更改为“^[A-Z]:\\\\$”,因为您必须为字符串转义一次反斜杠,为正则表达式引擎转义一次。这几乎是正确的。我必须使用
    @^[A-Z]:\\$
    。谢谢我甚至不知道它的存在!非常感谢!:)但是,它似乎没有提供一个只返回路径字符串最后一段的方法?您看到了吗?啊,似乎我误解了方法的名称和描述。就像一个符咒(有问题,但尾随反斜杠)!我甚至不知道它的存在!非常感谢!:)但是,它似乎没有提供一个只返回路径字符串最后一段的方法?您看到了吗?啊,似乎我误解了方法的名称和描述。就像一个符咒(有问题,但尾随反斜杠)!