当前位置: 代码迷 >> ASP.NET >> 正则表达式匹配有关问题
  详细解决方案

正则表达式匹配有关问题

热度:1909   发布时间:2013-02-25 00:00:00.0
正则表达式匹配问题
例如<a href="xw_read123"></a>
现在要把他变成<a href="xw_read123"></a>,前面连接的xw_read是不变的,只有后面的数字会变,现在就是把带有xw_read的链接后面加个

------解决方案--------------------------------------------------------
C# code
string tempStr = @"<a href=""xw_read129""></a>";                string pattern = @"(?i)(?<=<a[^>]*?href=(['""]?)[^'""]*?xw_read[^'""]*)(?=(['""]?)>)";                string result = Regex.Replace(tempStr, pattern, "");//<a href=\"xw_read123\"></a>
------解决方案--------------------------------------------------------
C# code
html=Regex.Replace(html,@"(?i)(?<=<a\b[^>]*?href=(['""]?)xw_read\d+)(?=\1[^>]*?>[^<>]*</a>","");
------解决方案--------------------------------------------------------
C# code
        private string ReplaceStr(Match m)        {            return m.Value+"";        }        private void button10_Click(object sender, EventArgs e)        {            StreamReader reader = new StreamReader("c:\\1.txt");            string source = reader.ReadToEnd();            Regex reg = new Regex(@"(?is)(?<=<a\s?href[^>].*?)xw_read[\d]+");            source = reg.Replace(source, ReplaceStr);        }
------解决方案--------------------------------------------------------
C# code
            string str = "<a href=\"xwread/xw_read1213\"></a>";            string result = Regex.Replace(str, @"(?is)(?<=<a[^>]*?href=(['""\s]?).*?xw_read\d+)(?=\1)", "");            Console.WriteLine(result);