首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

高分求正则啦!解决办法

2012-02-21 
高分求正则啦!C# codeclass Program{static void Main(string[] args){string a xxxx aabcdxxxxyyyxx

高分求正则啦!

C# code
class Program    {        static void Main(string[] args)        {            string a = "xxxx <a>abcdxxxxyyyxxx</a> <a>xxxxyyy</a> xxxyyyxx <img alt=xxx/> yyy";            Console.WriteLine(RegReplace(a, "yyy", "DDD"));            Console.Read();        }        public static string RegReplace(string content, string oldstr, string newstr)        {            //这个正则有问题,第一次出现在a标签中的yyy仍被替换为DDD,与预期不符。求帮忙修改!            return Regex.Replace(content, @"(?<!<a[^>]+>[^<]*)" + oldstr+ "(?![^(</a>)]*?</a>)", newstr, RegexOptions.IgnoreCase);        }      }


要求替换结果为所有不在a标签和img标签中的yyy变为DDD,即最终显示结果为:
xxxx <a>abcdxxxxyyyxxx</a> <a>xxxxyyy</a> xxxDDDxx <img alt=xxx/> DDD

全部的分了 谢谢大家

[解决办法]
C# code
            string a = "xxxx <a>abcdxxxxyyyxxx</a> <a>xxxxyyy</a> xxxyyyxx <img alt=xxx/> yyy";            Dictionary<string, string> dic = new Dictionary<string, string>();            int index=0;            foreach (Match match in Regex.Matches(a, "(<a>[^<]+</a>)|(<img[^>]+/>)"))            {                dic.Add("rep_" + index.ToString(), match.Value);                a = a.Replace(match.Value, "rep_" + index.ToString());                index++;            }            a = a.Replace("yyy", "DDD");            foreach (string s in dic.Keys)                a = a.Replace(s, dic[s]);            Console.WriteLine(a); 

热点排行