当前位置:编程学习 > C#/ASP.NET >>

高分求解文件名的正则表达式替换


文件名如/ABC/xxxxx.shtml
1)除了文件名里面包含Leaveword.shtml和-m-2.shtml的扩展名,其他的文件都要替换成/ABC/xxxxx.html格式

请问正则表达式怎么书写 正则表达式 --------------------编程问答-------------------- HTML文件代码里面有很多的超链接,其中有一种.shtml格式的超链接地址
现在要求如下:
1>特定知道的不需要替换的网址
  /Tools/Leaveword.shtml
  /Tools/OK.shtml
2>模糊但是需要替换的网址格式
  /ABC/xxxxxx.shtml需要替换为/ABC/xxxxxx.html,但是遇到/ABC/xxxxxx-m-2.shtml,不需要替换成/ABC/xxxxxx-m-2.html
  /CDE/xxxxxxx.shtml需要替换为/CDE/xxxxxxx.html,但是遇到/CDE/xxxxxx-m-2.shtml,不需要替换成/CDE/xxxxxx-m-2.html

请高手告知正则表达式怎么书写比较好。
   --------------------编程问答-------------------- string str="/Tools/Leaveword.shtml
  /Tools/OK.shtml
  /ABC/xxxxxx.shtml,ttryrt,/ABC/xxxxxx-m-2.shtml
  /CDE/xxxxxxx.shtml,";
str=Regex.Replace(str,"(?<=/x{1,}\.)shtml","html"); --------------------编程问答-------------------- To:Chinajiyong 

/ABC/xxxxxx.shtml,ttryrt,/ABC/xxxxxx-m-2.shtml
/CDE/xxxxxxx.shtml
补充说明
xxxxxx.shtml的xxxxxx是动态的字母,如abcdefg.shtml,dsajdsjajd.shtml等等,并不是说网页文件名称为:xxxxxx.shtml,可能有点难度,请问正则表达式如何书写。
--------------------编程问答--------------------
string str=@"/Tools/Leaveword.shtml
  /Tools/OK.shtml
  /ABC/xxxxxx.shtml,ttryrt,/ABC/xxxxxx-m-2.shtml
  /CDE/xxxxxxx.shtml,";
            str = Regex.Replace(str, @"(?i)(?<=/(?:(?![^.\s]*?(?:Leaveword|OK|-m-2))[^.]+?)\.)shtml", "html");


            /*
           /Tools/Leaveword.shtml
  /Tools/OK.shtml
  /ABC/xxxxxx.html,ttryrt,/ABC/xxxxxx-m-2.shtml
  /CDE/xxxxxxx.html,
             */
--------------------编程问答-------------------- 改一下就行了啊
(?is)(?<=/[a-z]{1,}\.)shtml


string str="/Tools/Leaveword.shtml
  /Tools/OK.shtml
  /ABC/xxxxxx.shtml,ttryrt,/ABC/xxxxxx-m-2.shtml
  /CDE/xxxxxxx.shtml,";
str=Regex.Replace(str,"(?is)(?<=/[a-z]{1,}\.)shtml","html");
--------------------编程问答-------------------- To:Return_false

当需要替换的文本只有一行字符串,没有问题,完全正确,但是需要替换的文本使用文件对象把一个网页拿出来的时候进行替换,如
            string filename = "QiYeYouJuBiaoZhunBan3G.html";
            FileStream file = new FileStream(Server.MapPath(filename), FileMode.Open);
            StreamReader streamReader = new StreamReader(file);
            string ReadText = streamReader.ReadToEnd();
就失去效果了,全部的.shtml被替换成了.html,调试了好久,非常奇怪,为什么一行有效,从一个网页里面调用进行替换就失效。

To:Chinajiyong
发现网址里面有数字,就保留.shtml格式,但是里面没有数字,就替换成.html格式。

我现在的需求就是需要保留的网址包含如下特征:
网址里面包含:[|代表或者包含]
Leaveword|-m-1|-m-2|-m-3|-m-4|-m-5|-m-6|-m-7|Rss-|News-|Product-|Project-|Help-|Agent-|Job-  这种格式
如xxxxxx-m-2.shtml,Product-absnei8d.shtml,News-absnei8d.shtml

请问正则表达式怎么书写。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,