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

📄 formregular.cs

📁 正则练习器
💻 CS
字号:
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;
using System.Threading;

namespace Regular.Helper
{
    public partial class FormRegular : Form
    {
        public FormRegular()
        {
            InitializeComponent();
        }

        private void FormRegular_Load(object sender, EventArgs e)
        {

        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            //Thread thread = new Thread(new ThreadStart(this.StartMatch));
            //thread.Start();

            StartMatch();
        }

        private void StartMatch()
        {
            txtResult.Text = "";

            string pattern = txtPattern.Text;

            Regex regex;

            MatchCollection matchs = this.Matchs(txtString.Text, pattern);

            foreach (Match match in matchs)
            {
                if (ckbClearHtml.Checked)
                    txtResult.Text += this.Replace(match.Value, "<[^<>]*>", "");
                else
                    txtResult.Text += match.Value;

                txtResult.Text += "\r\n";
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
        }

        private void btnClearRN_Click(object sender, EventArgs e)
        {
            txtString.Text = txtString.Text.Replace("\r", "\x20");
            txtString.Text = txtString.Text.Replace("\n", "\x20");
            txtString.Text = txtString.Text.Replace("\t", "\x20");

            while (txtString.Text.IndexOf("\x20\x20") >= 0)
            {
                txtString.Text = txtString.Text.Replace("\x20\x20", "\x20");
            }
        }

        private void ckbClearHtml_CheckedChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Matchs
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private MatchCollection Matchs(string text, string pattern)
        {
            Regex regex;

            MatchCollection matchs;

            try
            {
                regex = new Regex(pattern, RegexOptions.IgnoreCase);

                matchs = regex.Matches(text);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return matchs;
        }

        /// <summary>
        /// Matchs
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private Match Match(string text, string pattern)
        {
            Regex regex;

            Match match;

            try
            {
                regex = new Regex(pattern, RegexOptions.IgnoreCase);

                match = regex.Match(text);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return match;
        }

        /// <summary>
        /// Replace
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private string Replace(string text, string pattern, string replaceMent)
        {
            Regex regex;

            string match;

            try
            {
                regex = new Regex(pattern, RegexOptions.IgnoreCase);

                match = regex.Replace(text, replaceMent);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return match;
        }

    }
}

⌨️ 快捷键说明

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