0

我正在尝试在 rithtextbox 中以不同颜色突出显示 Xml 的正文部分和标签部分。

Regex regExp = new Regex("<[^>]+>");
foreach (Match match in regExp.Matches(richTextBox3.Text))
{
      richTextBox3.Select(match.Index, match.Length);
      richTextBox3.SelectionColor = Color.blue;
} 

regExp = new Regex("(?!<[^>]+>)");
foreach (Match match in regExp.Matches(richTextBox3.Text))
{
      richTextBox3.Select(match.Index, match.Length);
      richTextBox3.SelectionColor = Color.Green;
} 

问题是它与身体部位不匹配(标签除外)

4

1 回答 1

0

我在这里得到了答案是解决方案

Regex regExp = new Regex("<[^>]+>");
MatchCollection a = regExp.Matches(richTextBox3.Text);
for(int i=0;i<a.Count;i++)
{
     richTextBox3.Select(a[i].Index, a[i].Length);
     richTextBox3.SelectionColor = Color.Blue;
     if (i < a.Count - 1)
     {
         if (a[i+1].Index-(a[i].Index + a[i].Length + 1) > 0)
         {
              richTextBox3.Select(a[i].Index + a[i].Length , a[i + 1].Index - (a[i].Index + a[i].Length));

              richTextBox3.SelectionColor = Color.Green;

         }
     }
}
于 2012-12-13T10:08:32.010 回答