C#:正则表达式测试工具

功能:输入正则表达式即时匹配出结果

reg
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace RegexTool
{
    public partial class Form1 : Form
    {
        Regex reg;
        public Form1()
        {
            InitializeComponent();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.SelectionBackColor = Color.White;
            regIt();
        }
        private void regIt()
        {
            try
            {
                if (textBox1.Text == "")
                {
                    label1.Text = "";
                }
                else
                {
                    reg = new Regex(textBox1.Text);
                    MatchCollection mc = reg.Matches(@richTextBox1.Text);
                    int count = 0;
                    for (int i = 0; i < mc.Count; i++)
                    {
                        richTextBox1.Select(mc[i].Index, mc[i].Length);
                        if ((i + 2) % 2 == 0)
                        {
                            richTextBox1.SelectionBackColor = ColorTranslator.FromHtml("#FFF000");
                        }
                        else
                        {
                            richTextBox1.SelectionBackColor = ColorTranslator.FromHtml("#D2F854");
                        }
                        if (mc[i].Length != 0)
                        {
                            count++;
                        }
                    }
                    label1.Text = "结果:" + count + "个";
                }
            }
            catch (Exception ex)
            {
                label1.Text = "错误:" + ex.Message;
            }
        }
        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.V)
            {
                e.Handled = true;
                richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Text));
            }
        }
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (textBox1.Text == "在此输入正则表达式")
            {
                textBox1.Clear();
            }
        }
        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "在此输入正则表达式";
            }
        }
    }
}