如何使用Regex过滤c#中的文件名? [英] How to use Regex to filter file names in c#?

查看:56
本文介绍了如何使用Regex过滤c#中的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Regex reg = new Regex(@"^[0-9]*\.wav");
  Stack<string> wavefiles= new Stack<string>(Directory.GetFiles("c:\\WaveFiles", "*.wav").Where(path => reg.IsMatch(path)));

我在上面的代码中尝试做的是获取所有仅具有数字名称的wave文件.例如"123.wav"

what I am trying to do in the above code is to get all the wave files with names having only digits. for example "123.wav"

当我尝试上面的代码时,它不返回任何文件吗?

when I try the above code it does not return any files ?

任何想法我的代码有什么问题吗?

any ideas what is wrong with my code?

推荐答案

您也可以在不使用正则表达式的情况下做到这一点

You can also do it without Regex

var files = Directory.GetFiles("c:\\WaveFiles", "*.wav")
            .Where(f => Path.GetFileNameWithoutExtension(f).All(char.IsDigit));

这篇关于如何使用Regex过滤c#中的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆