Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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

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# .net linq和regex ismatch在何处_C#_Regex_Linq - Fatal编程技术网

C# .net linq和regex ismatch在何处

C# .net linq和regex ismatch在何处,c#,regex,linq,C#,Regex,Linq,在下面的C#方法中,我知道Directory.GetFileNsmes()确实返回文件列表。而且,我可以在Where contains(contact)中添加它。然而,就我个人而言,我无法确定searchPatter.IsMatch()为什么找不到文件。我已经在中测试了该模式,结果与预期相符。namePattern是“^\d{3}(.*.pdf”),应该有一个匹配项 public static List<string> GetFileNames(string pathName, s

在下面的C#方法中,我知道Directory.GetFileNsmes()确实返回文件列表。而且,我可以在Where contains(contact)中添加它。然而,就我个人而言,我无法确定searchPatter.IsMatch()为什么找不到文件。我已经在中测试了该模式,结果与预期相符。namePattern是“^\d{3}(.*.pdf”),应该有一个匹配项

 public static List<string> GetFileNames(string pathName, string namePattern, string contact)
 {
   var searchPattern = new Regex(namePattern, RegexOptions.IgnoreCase);
   var files = Directory.GetFiles(pathName).Where(f => searchPattern.IsMatch(f));
       //.Where(f => f.Contains(contact));
   return files.ToList();
 }
publicstaticlist getfilename(字符串路径名、字符串名称模式、字符串联系人)
{
var searchPattern=newregex(namePattern,RegexOptions.IgnoreCase);
var files=Directory.GetFiles(路径名).Where(f=>searchPattern.IsMatch(f));
//其中(f=>f.Contains(contact));
返回files.ToList();
}

如果这是已经回答了某处,请让我知道,但我一直无法找到它。我认为这是非常简单和直接的

Directory.GetFiles
将返回填充文件路径,该路径将是
Drive\Directory\file.ext
。这就是你的模式不匹配的原因。您需要
FileName
单独作为主题。试试这个

var files = Directory.GetFiles(pathName)
                 .Where(f => searchPattern.IsMatch(Path.GetFileName(f)));
GetFiles()返回附加到作为参数提供的路径的文件名列表。正则表达式是“^\d{3}(.*.pdf”),这是一个以三位数字开头的字符串。如果提供的字符串是绝对路径,则在Unix上以“/”开头,在Windows上以“C:\”开头;如果是相对路径,则以目录名开头。如果路径名只是一个空字符串,并且您正在搜索当前目录,那么代码就可以工作