您的位置:首页 > 编程语言 > C#

使用c#编写的正则表达式练习软件

2009-10-16 14:03 387 查看
在.NET FRAMEWORK的教学过程中,出于教学需要,使用C#写了这样一个正则表达式练习器软件,可以通过输入正则表达式和待分析的文本,由软件给出解析结果,其中解析结果里包括了分组信息,界面如下:



核心代码:

using System;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace RegexTester
{
    public partial class Form1 : Form
    {
        private const string resultCount = "发现 {0} 个";
        private string text = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(cboReg.Text) && !String.IsNullOrEmpty(txtSrc.Text))
            {
                StartProcess();
            }
            else
            {
                MessageBox.Show("正则表达式和待处理字符串不能为空!", "正则表达式练习器", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 使用选定的正则表达式对给定文字进行分析
        /// </summary>
        private void StartProcess()
        {
            Regex myRegex;
            MatchCollection matches;
            string pattern = cboReg.Text.Trim();
            string sourceText = txtSrc.Text.Trim();

            String[] groups;

            StringBuilder result = new StringBuilder();

            int foundItems = 0;

            try
            {
                myRegex = new Regex(@pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
                matches = myRegex.Matches(sourceText);
                foundItems = matches.Count;
                groups = chkGroup.Checked ? AnalyzeGroups(pattern) : myRegex.GetGroupNames();
                int num = 1;
                foreach (Match match in matches)
                {
                    result.Append(num++);
                    result.Append(".索引:");
                    result.Append(match.Index);
                    result.Append("/t");
                    result.Append(match.ToString());
                    if (groups.Length > 0)
                    {
                        for (int i = 0; i < groups.Length; i++)
                        {
                            result.Append("/r/n - 【");
                            result.Append(groups[i]);
                            result.Append("】:");
                            result.Append(match.Groups[groups[i]].Value);
                        }
                    }
                    else
                    {
                        result.Append("/t【无分组信息】");
                    }
                    result.Append("/r/n");
                }
            }
            catch
            {
                MessageBox.Show("正则表达式错误,请检查!", "正则表达式练习器", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                myRegex = null;
                matches = null;
            }

            txtResult.Text = result.ToString();
            lblResult.Text = string.Format(resultCount, foundItems);
        }

        /// <summary>
        /// 分析正则表达式中的组
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private static string[] AnalyzeGroups(string pattern)
        {
            const string groupPattern = @"/</D*/>";
            Regex myRegex;
            MatchCollection matches;

            try
            {
                myRegex = new Regex(groupPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
                matches = myRegex.Matches(pattern);

                string[] result = new string[matches.Count];

                int i = 0;
                foreach (Match match in matches)
                {
                    string group = match.ToString();
                    result[i] = group.Substring(1, group.Length - 2);
                    i++;
                }
                return result;
            }
            finally
            {
                myRegex = null;
                matches = null;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblResult.Text = string.Format(resultCount, 0);
        }

        private void cboReg_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboReg.SelectedIndex)
            {
                case 0:
                    txtSrc.Text =
                        "腾中重工拟投资8至20亿美元经营悍马品牌,/r/n据悉,按照我国的《境外投资管理办法》,1亿美元及以上的境外投资需要商务部审批。鉴于此,通用和腾中均表示,虽然双方已签署最终协议,但交易的最终完成还须等待中国和美国政府主管部门的审批。";
                    break;
                case 1:
                    txtSrc.Text = "﹣你所不知道的“战神”:155毫米加榴炮内部/r/n﹣中国新型155mm制导炮弹:20千米一发击毁坦克";
                    break;
                case 2:
                    txtSrc.Text = "公司名:微软公司,/r/n联系电话:(111)111-1111,(123)456-7890。";
                    break;
                default:
                    if (string.IsNullOrEmpty(text))
                    {
                        text = LoadTextFromFile("webpage.html");
                    }
                    txtSrc.Text = text;
                    break;
            }
        }

        /// <summary>
        /// 从文件加载大文本数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static string LoadTextFromFile(string file)
        {
            FileStream stream = null;
            StreamReader reader = null;
            String txtContent = "";
            try
            {
                stream = File.Open(file, FileMode.Open, FileAccess.Read);
                reader = new StreamReader(stream, Encoding.GetEncoding("gb2312"));
                txtContent = reader.ReadToEnd();
            }
            catch
            {
                MessageBox.Show("读取文件错误!", "正则表达式练习器", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (stream != null)
                {
                    if (stream.CanRead)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                    stream = null;
                }
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                    reader = null;
                }
            }
            return txtContent;
        }
    }
}


工程下载(Visual Studio 2008工程):

http://61.dc.ftn.qq.com/ftn_handler/e2c4956e7c666a884df97296e753544c9f6295d5ff61265a5f2dca70c1475cc3cbda3e2cf7fff0c0be9edfb899a99e51173ef2b82af463ea9bfe257ac442b301/RegexTester.rar?k=27326163ca7d9a9c84056d634366574c54510501525e065049515351554b515b52534c5a5053574e5c0758500750075106010453656965310155041b3103161701404f11041465
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: