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

📄 replace.cs

📁 学习正则表达式的编写
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace RegexTool
{
	/// <summary>
	/// Replace 的摘要说明。
	/// </summary>
	public class Replace : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtRegex;
		private System.Windows.Forms.TextBox txtRegexed;
		private System.Windows.Forms.TextBox txtText;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Replace()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.txtRegex = new System.Windows.Forms.TextBox();
			this.txtRegexed = new System.Windows.Forms.TextBox();
			this.txtText = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(68, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "匹配内容:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 60);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 1;
			this.label2.Text = "替换为:";
			// 
			// txtRegex
			// 
			this.txtRegex.BackColor = System.Drawing.SystemColors.Control;
			this.txtRegex.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtRegex.Location = new System.Drawing.Point(76, 20);
			this.txtRegex.Name = "txtRegex";
			this.txtRegex.Size = new System.Drawing.Size(272, 21);
			this.txtRegex.TabIndex = 2;
			this.txtRegex.Text = "";
			// 
			// txtRegexed
			// 
			this.txtRegexed.BackColor = System.Drawing.SystemColors.Control;
			this.txtRegexed.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtRegexed.Location = new System.Drawing.Point(76, 56);
			this.txtRegexed.Name = "txtRegexed";
			this.txtRegexed.Size = new System.Drawing.Size(272, 21);
			this.txtRegexed.TabIndex = 3;
			this.txtRegexed.Text = "";
			// 
			// txtText
			// 
			this.txtText.BackColor = System.Drawing.SystemColors.Control;
			this.txtText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtText.Location = new System.Drawing.Point(76, 96);
			this.txtText.Multiline = true;
			this.txtText.Name = "txtText";
			this.txtText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtText.Size = new System.Drawing.Size(272, 132);
			this.txtText.TabIndex = 4;
			this.txtText.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 100);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(52, 23);
			this.label3.TabIndex = 5;
			this.label3.Text = "文本:";
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button1.Location = new System.Drawing.Point(148, 232);
			this.button1.Name = "button1";
			this.button1.TabIndex = 6;
			this.button1.Text = "替换";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Replace
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(368, 259);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.txtText);
			this.Controls.Add(this.txtRegexed);
			this.Controls.Add(this.txtRegex);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Replace";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "替换练习";
			this.TopMost = true;
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				if ( txtRegex.Text!="" && txtRegexed.Text!="" && txtText.Text!="" )
				{
					string strRegex = @txtRegex.Text;
					string strRegexed = @txtRegexed.Text;
					string strText = txtText.Text;
					string temp;
					temp = Regex.Replace( strText, strRegex, strRegexed );
					txtText.Text = temp;
					
				}
				else
				{
					MessageBox.Show("匹配内容,替换为,文本不能为空!","错误提示!");
				}
			}
			catch( Exception ee )
			{
				MessageBox.Show( ee.Message.ToString() );
			}
		}
	}
}

⌨️ 快捷键说明

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