日期:2014-05-17  浏览次数:20598 次

.net如何删除名称部分相同的所有图片
如:
图片A的名称是: 12345-100.jpg
图片B的名称是: 12345-200.jpg

现在我知道的字符是12345,怎么像正则表达那样把图片A和B匹配出来,然后删除。

注:后面的100和200可能是其它的,不能是直接12345然后加上后面的,要的是所有含有12345的图片的集合,谢谢各位了!

------解决方案--------------------
C# code
  string html = @"
图片A的名称是: 12345-100.jpg
图片B的名称是: 12345-200.jpg
            ";
            foreach (Match m in Regex.Matches(html, @"12345\-\d+?\.(jpg|gif|png)", RegexOptions.IgnoreCase))
            {
                Response.Write(m.Value);
            }//取出来
            //删除用下面的
            html= Regex.Replace(html, @"12345\-\d+?\.(jpg|gif|png)","", RegexOptions.IgnoreCase);
            Response.Write(html);

------解决方案--------------------
C# code
string pre_str="12345";//定义查找字符串
                string split_str="-";//定义之间的分隔符
                string str_Extension=".jpg";//定义图片后缀
                string pattern_img=@"(?i)"+pre_str+split_str+@"[^.]*?"+str_Extension.Replace(".",@"\.");//定义匹配正则
                string path = @"c:\\";//定义路径
                DirectoryInfo dir = new DirectoryInfo(path);
                //获取符合匹配的图片
                List<FileInfo> list_image_file = dir.GetFiles().Where(a => a.Extension.Equals(str_Extension) && Regex.IsMatch(a.Name, pattern_img)).ToList() ;
                foreach (FileInfo fi in list_image_file)
                {
                    File.Delete(fi.FullName);//开始删除
                }

------解决方案--------------------

if ("".StartsWith("12345") && "".EndsWith(".jpg"))
{
//删除
}
------解决方案--------------------
方式很多,同意楼上的,使用正则去判断或者用字符串匹配方式,都可以
------解决方案--------------------
{\d+}\-\d+?\.(jpg|gif|png)
用不用分组啊?