gpt4 book ai didi

c# - .net linq with regex ismatch in where

转载 作者:太空宇宙 更新时间:2023-11-03 20:08:49 26 4
gpt4 key购买 nike

在下面的 C# 方法中,我知道 Directory.GetFileNsmes() 会返回文件列表。而且,我可以添加有效的 Where contains(contact) 。然而,对于我的生活,我无法确定为什么 searchPatter.IsMatch() 无法找到文件。我测试了 http://regexpal.com/ 中的模式它如预期的那样 qorks。 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();
}

如果这已经在某处得到解答,请告诉我,但我找不到它。我认为这非常简单直接。

最佳答案

Directory.GetFiles 将返回填充文件路径,即 Drive\Directory\File.ext。这就是为什么你的模式似乎不匹配。您只需要 FileName 作为主题。试试这个

var files = Directory.GetFiles(pathName)
.Where(f => searchPattern.IsMatch(Path.GetFileName(f)));

关于c# - .net linq with regex ismatch in where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com