求教一段简单的正则表达式,
在下面这个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\"");}

解决方案 »

  1.   

    private void Form1_Load(object sender, System.EventArgs e)
    {
    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;
    }
      

  2.   

    匹配一个单词边界,也就是指单词和空格间的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。
      

  3.   

    试一下:(?<=(<table|<td)[^>]*?)txsTdTextOnlyMoreStyle(?=[\s\S]*?>)
      

  4.   

    哎,你打开记事本,查找txsTdTextOnlyMoreStyle,替换为class="s2",
      

  5.   

    用这个: @"(<td\s|<table\s)(txsTdTextOnlyMoreStyle)\s"代码: this.textBox2.Text = Regex.Replace(this.textBox1.Text, @"(<td\s|<table\s)(txsTdTextOnlyMoreStyle)\s", "$1" + " class=s1");