正则表达式来查找文件夹中的文件文件、夹中、正则表达式

2023-09-04 22:38:32 作者:我的霸道只對你

如何找到的所有文件夹中匹配一个正则表达式?

How to find all files matches a regex pattern in a folder?

感谢

推荐答案

在的GetFiles 方法允许您指定通配符模式,但不是一个真正的正则表达式。另一种可能性是通过文件简单地循环,并验证其名称针对正则表达式

The GetFiles method allows you to specify a wildcard pattern but not really a regex. Another possibility is to simply loop through the files and validate their name against a regex.

IEnumerable<string> files = Directory
    .EnumerateFiles(@"c:\somePath")
    .Where(name => Regex.IsMatch(name, "SOME REGEX"));