求教一段简单的正则表达式,
在下面这个td或table中,
<td txsTdTextOnlyMoreStyle colspan="2" bgcolor="#ff0000" ...>
我只想把“txsTdTextOnlyMoreStyle”替换成class="s2",而其它的保持不变,比如:
<td class="s2" colspan="2" bgcolor="#ff0000" ...>
应该怎么做?谢谢。下面我这样做,结果把td中的所有内容都替换掉了。
string s = "<td txsTdTextOnlyMoreStyle colspan="2" bgcolor="#ff0000" ...>";
string pattern = @"(?<=[<table|<td][\s*])(["+RegExStr+"]*?)(?=>)";
Match um = Regex.Match(s, pattern, RegexOptions.Singleline);
if(um.Success)
{
s = Regex.Replace(s, pattern, "class=\"s1\"");}
在下面这个td或table中,
<td txsTdTextOnlyMoreStyle colspan="2" bgcolor="#ff0000" ...>
我只想把“txsTdTextOnlyMoreStyle”替换成class="s2",而其它的保持不变,比如:
<td class="s2" colspan="2" bgcolor="#ff0000" ...>
应该怎么做?谢谢。下面我这样做,结果把td中的所有内容都替换掉了。
string s = "<td txsTdTextOnlyMoreStyle colspan="2" bgcolor="#ff0000" ...>";
string pattern = @"(?<=[<table|<td][\s*])(["+RegExStr+"]*?)(?=>)";
Match um = Regex.Match(s, pattern, RegexOptions.Singleline);
if(um.Success)
{
s = Regex.Replace(s, pattern, "class=\"s1\"");}
{
string s =@"<td txsTdTextOnlyMoreStyle colspan=""2"" bgcolor=""#ff0000"" ...>";
this.textBox1.Text=s;
} private void button1_Click(object sender, System.EventArgs e)
{
string s=this.textBox1.Text.Trim(); string myreg=@"(?<=<td |<table )\btxsTdTextOnlyMoreStyle\b";
string a=Regex.Replace(s,myreg,new MatchEvaluator(this.mydo),System.Text.RegularExpressions.RegexOptions.IgnoreCase);
MessageBox.Show("ok","dazhu");
this.textBox1.Text=a;
} private string mydo(Match m)
{
string a=m.Value;
string c=@"class=""s2""";
return c;
}