开发者

How to find files according RegEx in C#

开发者 https://www.devze.com 2022-12-29 17:34 出处:网络
I need to get list of files on some drive with paths that matches specific pattern, for example FA\\开发者_JAVA百科d\\d\\d\\d.xml where \\d is digit (0,1,2..9). So files can have names like FA5423.xml

I need to get list of files on some drive with paths that matches specific pattern, for example FA\开发者_JAVA百科d\d\d\d.xml where \d is digit (0,1,2..9). So files can have names like FA5423.xml.

What is the most efficient name to do this?


Are you using C# 3?

Regex reg = new Regex(@"^FA[0-9]{4}\.xml$");
var files = Directory.GetFiles(yourPath, "*.xml").Where(path => reg.IsMatch(path));


You could do something like:

System.IO.Directory.GetFiles(@"C:\", "FA????.xml", SearchOption.AllDirectories);

Then from your results just iterate over them and verify them against your regex i.e. that the ? characters in the name are all numbers


Use the Directory API to find FA????.xml, and run the resulting list through a regex filter:

var fa = from f in Directory.GetFiles("FA????.xml")
         where Regex.Match(f, "FA\d\d\d\d\.xml")
         select f;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号