我试图在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;
}
问题是它与身体部位(标签除外)不匹配
答案 0 :(得分: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;
}
}
}