c# 寻找字符串中指定 字段 替换
string a1={ <td style="TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-RIGHT: red 1px solid"><IMG style="CURSOR: hand" id=0.1869366964525968 onclick=openyz(0.1869366964525968,201211189789) name=0.1869366964525968 alt="c# 寻觅字符串中指定 字段 替换" src="/images/123.gif"> </td>
<td style="TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-RIGHT: red 1px solid"><IMG style="CURSOR: hand" id=0.5159706003032625 onclick=openyz(0.5159706003032625,201211187808) name=0.5159706003032625 alt="c# 寻觅字符串中指定 字段 替换" src="/images/123.gif"> </td>
<td style="TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-RIGHT: red 1px solid"><IMG style="CURSOR: hand" id=0.1869366964525968 onclick=openyz(0.1869366964525968,201211189791) name=0.1869366964525238 alt="c# 寻觅字符串中指定 字段 替换" src="/images/123.gif"> </td> }
string a2= "201211187808";
如上两个字符串 在 a1 中 查找 a2 如果相等 则将 后面的 src="/images/123.gif" 替换为 src="/images/1234.gif"
各位大虾 请赐教
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a1 = @"<td style=""TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-RIGHT: red 1px solid""><IMG style=""CURSOR: hand"" id=0.1869366964525968 onclick=openyz(0.1869366964525968,201211189789) name=0.1869366964525968 alt="c# 寻觅字符串中指定 字段 替换""" src=""/images/123.gif""> </td>
<td style=""TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-RIGHT: red 1px solid""><IMG style=""CURSOR: hand"" id=0.5159706003032625 onclick=openyz(0.5159706003032625,201211187808) name=0.5159706003032625 alt="c# 寻觅字符串中指定 字段 替换""" src=""/images/123.gif""> </td>
<td style=""TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-RIGHT: red 1px solid""><IMG style=""CURSOR: hand"" id=0.1869366964525968 onclick=openyz(0.1869366964525968,201211189791) name=0.1869366964525238 alt="c# 寻觅字符串中指定 字段 替换""" src=""/images/123.gif""> </td>";
string a2 = "201211187808";
string result = Regex.Replace(a1, "(?is)" + a2 + @".+?/images/123.gif", x => x.Value.Replace("123.gif", "1234.gif"));
Console.WriteLine(result);
}
}
}
<td style="TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-R
IGHT: red 1px solid"><IMG style="CURSOR: hand" id=0.1869366964525968 onclick=ope
nyz(0.1869366964525968,201211189789) name=0.1869366964525968 alt="c# 寻觅字符串中指定 字段 替换" src="/images
/123.gif"> </td>
<td style="TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1
px solid; BORDER-RIGHT: red 1px solid"><IMG style="CURSOR: hand" id=0.5159706003
032625 onclick=openyz(0.5159706003032625,201211187808) name=0.5159706003032625 a
lt="" src="/images/1234.gif"> </td>
<td style="TEXT-ALIGN: center; HEIGHT: 40px; BORDER-TOP: red 1px solid; BORDER-R
IGHT: red 1px solid"><IMG style="CURSOR: hand" id=0.1869366964525968 onclick=ope
nyz(0.1869366964525968,201211189791) name=0.1869366964525238 alt="c# 寻觅字符串中指定 字段 替换" src="/images
/123.gif"> </td>
Press any key to continue . . .
[解决办法]
string a1="你那一坨";
string a2="201211187808";
a1=Regex.Replace(a1,@"(?i)(?<=<img\s[^>]*?"+a2+@"[^>]*?src=(['""]?))[^'""<>]+(?=\1)","/images/1234.gif");