Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c中用于文件夹路径验证(远程路径、FTP路径、本地系统文件夹路径等)的单个正则表达式#_C#_Regex_Path_Directory - Fatal编程技术网

C# c中用于文件夹路径验证(远程路径、FTP路径、本地系统文件夹路径等)的单个正则表达式#

C# c中用于文件夹路径验证(远程路径、FTP路径、本地系统文件夹路径等)的单个正则表达式#,c#,regex,path,directory,C#,Regex,Path,Directory,我希望在C#中使用单个正则表达式进行文件夹路径验证(远程路径、FTP路径、本地系统文件夹路径等) 例如: c:\folder one\folder2\folder 3 \\remoteMachine\folder1 \\1.22.33.444\文件夹1\folder2 ftp://12.123.112.231/ C:\ProgramFiles\Google\Chrome 试试这个 using System; using System.Collections.Generic; using Syst

我希望在C#中使用单个正则表达式进行文件夹路径验证(远程路径、FTP路径、本地系统文件夹路径等)

例如:

  • c:\folder one\folder2\folder 3
  • \\remoteMachine\folder1
  • \\1.22.33.444\文件夹1\folder2
  • ftp://12.123.112.231/
  • C:\ProgramFiles\Google\Chrome
  • 试试这个

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                string[] inputs = {
                      @"c:\folder one\folder2\folder 3",
                      @"\\remoteMachine\folder1",
                      @"\\1.22.33.444\folder 1\folder2",
                      @"ftp://12.123.112.231/",
                      @"C:\Program Files\Google\Chrome"
                                 };
                string ip = @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
                string pattern = string.Format( @"^[A-Z]:(\\(\w+\s*)+)+" + // filename
                                 @"|^\\\\{0}(\\(\w+\s*)+)+" +              // url with ip
                                 @"|^\\(\\(\w+\s*)+)+" +                   // network filename \\abc\def
                                 @"|^FTP://{0}/" +                         // ftp with ip
                                 @"|^FTP://[A-Z]\w+/", ip);                // ftp with hostname
    
    
                foreach (string input in inputs)
                {
                    bool match = Regex.IsMatch(input,pattern, RegexOptions.IgnoreCase);
                    Console.WriteLine("\"{0}\" {1}", input, match? "matches" : "does not match");
                }
                Console.ReadLine();
            }
        }
    }
    ​
    

    “文件夹路径验证”可能重复-为什么?只需尝试访问指定的路径。有效字符串仍然可以指向不存在或不可用的资源。请尝试解释您的答案…需要解释什么?对于任何理解基本正则表达式的人来说,这是显而易见的。Regex上的大多数帖子该人知道Regex,但不知道确切的语法。“需要解释什么?”-你编写的正则表达式背后的想法,这样有人可以验证他们是否正确地实现了这些原则,并在以后找到这篇帖子时加以改进。例如,支持使用主机名而不是IP的FTP URI。请阅读我对“试试这个”答案的立场。你不必同意,我只是想解释一下,我认为在没有解释的情况下转储代码对其他人可能不会像你想的那样有用。添加了注释并包含了带有hostname.jdweng的FTP感谢工作正常。。。。