string input = "msg";
Match match;
Regex regex = new Regex(@"(\[color=([\S]+)\])([ \S\t\]*?)(\[\/color\])", RegexOptions.IgnoreCase);
for (match = regex.Match(input); match.Success; match = match.NextMatch())
{
  input = input.Replace(match.Groups[0].ToString(), "<FONT COLOR=" + match.Groups[2].ToString() + ">" + match.Groups[3].ToString() + "</FONT>");
}这样可以得到正常的值为:<FONT COLOR=#FF0000>msg</FONT>但是
当input = "msgmsg2";得到的却是:<FONT COLOR=#FF0000]msg[/color][color=#FFee00>msg2</FONT>请问有什么办法可以解决这一问题?

解决方案 »

  1.   

    上面由于解释了UBB,可能看看不清,请看下面的string input = "msg"; 
    Match match; 
    Regex regex = new Regex(@"(\[color=([\S]+)\])([ \S\t\]*?)(\[\/color\])", RegexOptions.IgnoreCase); 
    for (match = regex.Match(input); match.Success; match = match.NextMatch()) 

      input = input.Replace(match.Groups[0].ToString(), " <FONT COLOR=" + match.Groups[2].ToString() + ">" + match.Groups[3].ToString() + " </FONT>"); 
    } 这样可以得到正常的值为: <FONT COLOR=#FF0000>msg </FONT> 但是 
    当input = "msgmsg2"; 得到的却是: <FONT COLOR=#FF0000]msg[/color][color=#FFee00>msg2 </FONT> 请问有什么办法可以解决这一问题?
      

  2.   


    string input = "msgmsg2";
                string result = Regex.Replace(input, @"\[color=([^]]*)\](\w*)\[/color\]", new MatchEvaluator(delegate(Match match) { return "<FONT COLOR=" + match.Groups[1] + ">" + match.Groups[2] + "</FONT>"; }));
                Console.WriteLine(result);这个不太准的,应该要用平衡组,但是平衡组我不是很熟
      

  3.   


                string input = "msg msg2";    
                Regex   regex   =   new   Regex(@"\[\s*color\s*=([^]]*)\]([^[]+)\[\s*\/\s*color\s*\]",RegexOptions.IgnoreCase);
                MatchCollection mc = regex.Matches(input);
                foreach (Match m in mc)
                {
                    string output = regex.Replace(input, "<FONT COLOR=$1>$2</FONT>");
                    Console.WriteLine(output);
                } 
      

  4.   

    (\[color=([\S]+)\])[\S\t]*(\[/color\])试试
      

  5.   

    string input = "msgmsg2";
    input = Regex.Replace(input, @"\[(color)=([^\]]+)\]([^\[]*)\[/\1\]", "<font color='${2}'>${3}</font>", RegexOptions.IgnoreCase);//<font color='#FF0000'>msg</font><font color='#FFee00'>msg2</font>
      

  6.   

    string input = "msgmsg2";   
    Match match;
    Regex regex = new Regex(@"(\[color=((?:(?!\[color)[\S])+?)\])([ \S\t]*?)\[\/color\]", RegexOptions.IgnoreCase);
    for (match = regex.Match(input); match.Success; match = match.NextMatch())
    {
        input = input.Replace(match.Groups[0].ToString(), "<FONT COLOR= " + match.Groups[2].ToString() + "> " + match.Groups[3].ToString() + "</FONT> ");
    }
    Console.WriteLine(input);
      

  7.   

    你的那个出问题就出在了([\S]+)上,匹配优先~~用否定正向环视判断一个位置,位置之后不为[color=,再进行就可以了~~Regex regex = new Regex(@"(\[color=((?:(?!\[color)[\S])+?)\])([ \S\t]*?)\[\/color\]", RegexOptions.IgnoreCase);
      

  8.   

    input = Regex.Replace(input, @"\[(color)=([^\]]+)\]([^\[]*)\[/\1\]", "<font color='${2}'>${3}</font>", RegexOptions.IgnoreCase);
    MessageBox.Show(input);//结果<font color='#FF0000'>msg</font><font color='#FFee00'>msg2</font>
      

  9.   


    input = Regex.Replace(input, @"\[(color)=([^\]]+)\]([\s\S]*?)\[/\1\]", "<font color='${2}'>${3}</font>", RegexOptions.IgnoreCase);
                MessageBox.Show(input);
      

  10.   

    [color=#FF0000]testtest[/color]