⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmregex.cs

📁 正则表达式工具,C#.NET开发.功能虽不强大.但能够解决工作实际的应用.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace RegexTest
{
    public partial class frmRegex : Form
    {
        public frmRegex()
        {
            InitializeComponent();
        }

        //保存正则表达式
        private void SaveRegexButton_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog(); 
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            StreamWriter streamWriterRegex = File.CreateText(saveFileDialog1.FileName);
            streamWriterRegex.Write(this.ResultsTextBox.Text);
            streamWriterRegex.Close();
        }

        //读取正则表达式
        private void OpenRegexButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            StreamReader streamReaderRegex = File.OpenText(openFileDialog1.FileName);
            this.InputTextBox.Text = streamReaderRegex.ReadToEnd();
            streamReaderRegex.Close();
        }

        //获取正则表达式的匹配参数
        private RegexOptions GetSelectedRegexOptions()
        {
            RegexOptions selectedRegexOptions = RegexOptions.None;

            if (this.IgnoreCaseChkBox.Checked)
                selectedRegexOptions |= RegexOptions.IgnoreCase;
            if (this.ExplicitCaptureChkBox.Checked)
                selectedRegexOptions |= RegexOptions.ExplicitCapture;
            if (this.ECMAScriptChkBox.Checked)
                selectedRegexOptions |= RegexOptions.ECMAScript;
            if (this.IgnoreCaseChkBox.Checked)
                selectedRegexOptions |= RegexOptions.IgnoreCase;
            if (this.MultiLineChkBox.Checked)
                selectedRegexOptions |= RegexOptions.Multiline;
            if (this.RightToLeftChkBox.Checked)
                selectedRegexOptions |= RegexOptions.RightToLeft;
            if (this.SingeLineChkBox.Checked)
                selectedRegexOptions |= RegexOptions.Singleline;
            return selectedRegexOptions;

        }

        private void TestRegexButton_Click(object sender, EventArgs e)
        {
            try
            {
                RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
                Regex testRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
                if (testRegex.IsMatch(this.InputTextBox.Text))
                {
                    this.ResultsTextBox.ForeColor = Color.Black;
                    this.ResultsTextBox.Text = "匹配成功";
                }
                else
                {
                    this.ResultsTextBox.ForeColor = Color.Red;
                    this.ResultsTextBox.Text = "匹配失败";
                }
            }
            catch (ArgumentException ex)
            {
                this.ResultsTextBox.ForeColor = Color.Red;
                this.ResultsTextBox.Text = "错误:"+ex.Message;
            }
        }

        private void ReplaceButton_Click(object sender, EventArgs e)
        {
            try
            {
                RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
                Regex replaceRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);

                this.ResultsTextBox.ForeColor = Color.Black;
                this.ResultsTextBox.Text = replaceRegex.Replace(this.InputTextBox.Text, this.ReplacementTextBox.Text);
                              
            }
            catch (ArgumentException ex)
            {
                this.ResultsTextBox.ForeColor = Color.Red;
                this.ResultsTextBox.Text = "错误:" + ex.Message;
            }
        }

        private void SplitBoutton_Click(object sender, EventArgs e)
        {
            try
            {
                RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
                Regex splitRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);

                String[] splitResults;
                splitResults = splitRegex.Split(this.InputTextBox.Text);
                StringBuilder resultsString = new StringBuilder(this.InputTextBox.Text.Length);

                foreach (String stringElement in splitResults)
                    resultsString.Append(stringElement + Environment.NewLine);

                this.ResultsTextBox.ForeColor = Color.Black;
                this.ResultsTextBox.Text = resultsString.ToString();
            }
            catch (ArgumentException ex)
            {
                this.ResultsTextBox.ForeColor = Color.Red;
                this.ResultsTextBox.Text = "错误:" + ex.Message;
            }            

        }

        private void MatchesButton_Click(object sender, EventArgs e)
        {
            try
            {
                RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
                Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);

                MatchCollection matchesFound;
                matchesFound = matchesRegex.Matches(this.InputTextBox.Text);

                String nextMath = "------- 下一个匹配项 ----------\r\n";
                StringBuilder resultsString = new StringBuilder(64);

                foreach(Match matchMode in matchesFound)
                    resultsString.Append(matchMode.Value +(Environment.NewLine+nextMath));

                this.ResultsTextBox.ForeColor = Color.Black;
                this.ResultsTextBox.Text = resultsString.ToString();
            }
            catch (ArgumentException ex)
            {
                this.ResultsTextBox.ForeColor = Color.Red;
                this.ResultsTextBox.Text = "错误:" + ex.Message;
            }            

        }

        private void GroupsButton_Click(object sender, EventArgs e)
        {
            try
            {
                RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
                Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);

                MatchCollection matchesFound;
                matchesFound = matchesRegex.Matches(this.InputTextBox.Text);

                String nextMath = "------- 下一个分组 ----------\r\n";
                StringBuilder resultsString = new StringBuilder(64);
                GroupCollection matchGroups;


                foreach (Match matchMode in matchesFound)
                {
                    matchGroups = matchMode.Groups;
                    foreach (Group matchGroup in matchGroups)
                        resultsString.Append("(" + matchGroup.Value + ")");
                    resultsString.Append(Environment.NewLine + nextMath);
                }

                this.ResultsTextBox.ForeColor = Color.Black;
                this.ResultsTextBox.Text = resultsString.ToString();
            }
            catch (ArgumentException ex)
            {
                this.ResultsTextBox.ForeColor = Color.Red;
                this.ResultsTextBox.Text = "错误:" + ex.Message;
            }           
        }

        //常见正则表达式
        private void FamiliarRegex_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MenuRegex.Show(this,new Point(FamiliarRegex.Location.X, FamiliarRegex.Location.Y + FamiliarRegex.Size.Height));  
            //MessageBox.Show("sdfsd");
        }

        private void MenuRegexItem_Click(object sender, EventArgs e)
        {
            string strRegex = "";
            switch (((ToolStripMenuItem)sender).Text)
            {
                case "整数":
                    strRegex = @"^((\+|-)\d)?\d*$";
                    break;
                case "浮点数":
                    strRegex = @"^(?:\+|-)?\d+(?:\.\d+)?$";
                    break;
                case "电话号码":
                    strRegex = @"\d{3}-\d{8}|\d{4}-\d{7}";
                    break;
                case "邮政编码":
                    strRegex = @"[1-9]\d{5}(?!\d)";
                    break;
                case "Email地址1":
                    strRegex = @"^(([^<>()\[\]\\.,;:@"+'"'+@"\x00-\x20\x7F]|\\.)+)@(([a-z0-9-]|#\d+?)*([a-z0-9]|#\d+?)\.)+([a-z]{2,4})";
                    break;
                case "Email地址2":
                    strRegex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                    break;
                case "IP地址":
                    strRegex = @"^((\d|[1-9]\d|1??\d{1,2}\d{1,2}|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1??\d{1,2}\d{1,2}|2[0-4]\d|25[0-5])$";
                    break;
                case "中文字符":
                    strRegex = @"[\u4e00-\u9fa5]";
                    break;
                case "空白行":
                    strRegex = @"\n\s*\r";
                    break;
                case "HTML标记":
                    strRegex = @"<(\S*?)[^>]*>.*?</\1>|<.*? />";
                    break;
                case "首尾空白字符":
                    strRegex = @"^\s*|\s*$";
                    break;
                case "网址URL":
                    strRegex = @"[a-zA-z]+://[^\s]*";
                    break;
                case "网络帐号":
                    strRegex = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
                    break;
                case "国内电话号码":
                    strRegex = @"\d{3}-\d{8}|\d{4}-\d{7}";
                    break;
                case "腾讯QQ号":
                    strRegex = @"[1-9][0-9]{4,}";
                    break;
                case "中国身份证号":
                    strRegex = @"\d{15}|\d{18}";
                    break;
            }
            RegexTextBox.Text = strRegex;
        }







    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -