一些常用的 正则表达式,区别于网上其他式子。

本文介绍了一个正则表达式练习工具,提供了多种常见正则表达式模式,并演示了如何使用这些模式进行文本匹配。此外,文章还包含了对正则表达式的详细解释及其在不同场景下的应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个表达式练习/验证工具,顺便写入了我常用的一些式子。

================================================================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
namespace 正则表达式练习工具
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private Hashtable ht = new Hashtable();
   private System.Windows.Forms.GroupBox groupBox1;
   private System.Windows.Forms.GroupBox groupBox2;
   private System.Windows.Forms.Button btnPiPei;
   private System.Windows.Forms.Button btnClose;
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.TextBox tbRegixContent;
   private System.Windows.Forms.Label label2;
   private System.Windows.Forms.TextBox tbResult;
   private ArrayList al = new ArrayList();
   private ArrayList al1 = new ArrayList();
   private System.Windows.Forms.TextBox tbContents;
   private System.Windows.Forms.TextBox tbResult1;
   private System.Windows.Forms.ComboBox comboBox1;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;

   public Form1()
   {
    //
    // 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()
   {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.label2 = new System.Windows.Forms.Label();
    this.tbContents = new System.Windows.Forms.TextBox();
    this.tbRegixContent = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.groupBox2 = new System.Windows.Forms.GroupBox();
    this.tbResult1 = new System.Windows.Forms.TextBox();
    this.tbResult = new System.Windows.Forms.TextBox();
    this.btnPiPei = new System.Windows.Forms.Button();
    this.btnClose = new System.Windows.Forms.Button();
    this.comboBox1 = new System.Windows.Forms.ComboBox();
    this.groupBox1.SuspendLayout();
    this.groupBox2.SuspendLayout();
    this.SuspendLayout();
    //
    // groupBox1
    //
    this.groupBox1.Controls.Add(this.label2);
    this.groupBox1.Controls.Add(this.tbContents);
    this.groupBox1.Controls.Add(this.tbRegixContent);
    this.groupBox1.Controls.Add(this.label1);
    this.groupBox1.Location = new System.Drawing.Point(16, 8);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(536, 176);
    this.groupBox1.TabIndex = 0;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "输入";
    //
    // label2
    //
    this.label2.ForeColor = System.Drawing.Color.Brown;
    this.label2.Location = new System.Drawing.Point(8, 72);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(56, 23);
    this.label2.TabIndex = 3;
    this.label2.Text = "输入内容";
    //
    // tbContents
    //
    this.tbContents.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.tbContents.ForeColor = System.Drawing.Color.Brown;
    this.tbContents.Location = new System.Drawing.Point(64, 63);
    this.tbContents.Multiline = true;
    this.tbContents.Name = "tbContents";
    this.tbContents.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.tbContents.Size = new System.Drawing.Size(464, 97);
    this.tbContents.TabIndex = 2;
    this.tbContents.Text = "";
    //
    // tbRegixContent
    //
    this.tbRegixContent.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.tbRegixContent.ForeColor = System.Drawing.Color.Brown;
    this.tbRegixContent.Location = new System.Drawing.Point(64, 24);
    this.tbRegixContent.Name = "tbRegixContent";
    this.tbRegixContent.Size = new System.Drawing.Size(464, 26);
    this.tbRegixContent.TabIndex = 1;
    this.tbRegixContent.Text = "";
    //
    // label1
    //
    this.label1.ForeColor = System.Drawing.Color.Brown;
    this.label1.Location = new System.Drawing.Point(8, 32);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(56, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "表达式";
    //
    // groupBox2
    //
    this.groupBox2.Controls.Add(this.tbResult1);
    this.groupBox2.Controls.Add(this.tbResult);
    this.groupBox2.Location = new System.Drawing.Point(16, 184);
    this.groupBox2.Name = "groupBox2";
    this.groupBox2.Size = new System.Drawing.Size(536, 248);
    this.groupBox2.TabIndex = 1;
    this.groupBox2.TabStop = false;
    this.groupBox2.Text = "输出";
    //
    // tbResult1
    //
    this.tbResult1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.tbResult1.ForeColor = System.Drawing.Color.Brown;
    this.tbResult1.Location = new System.Drawing.Point(0, 136);
    this.tbResult1.Multiline = true;
    this.tbResult1.Name = "tbResult1";
    this.tbResult1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.tbResult1.Size = new System.Drawing.Size(536, 104);
    this.tbResult1.TabIndex = 4;
    this.tbResult1.Text = "";
    //
    // tbResult
    //
    this.tbResult.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.tbResult.ForeColor = System.Drawing.Color.Brown;
    this.tbResult.Location = new System.Drawing.Point(3, 17);
    this.tbResult.Multiline = true;
    this.tbResult.Name = "tbResult";
    this.tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.tbResult.Size = new System.Drawing.Size(530, 116);
    this.tbResult.TabIndex = 3;
    this.tbResult.Text = "";
    //
    // btnPiPei
    //
    this.btnPiPei.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.btnPiPei.Location = new System.Drawing.Point(56, 448);
    this.btnPiPei.Name = "btnPiPei";
    this.btnPiPei.Size = new System.Drawing.Size(128, 32);
    this.btnPiPei.TabIndex = 2;
    this.btnPiPei.Text = "匹 配 (&P)";
    this.btnPiPei.Click += new System.EventHandler(this.btnPiPei_Click);
    //
    // btnClose
    //
    this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.btnClose.Location = new System.Drawing.Point(208, 448);
    this.btnClose.Name = "btnClose";
    this.btnClose.Size = new System.Drawing.Size(136, 32);
    this.btnClose.TabIndex = 3;
    this.btnClose.Text = "关 闭 (&C)";
    this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
    //
    // comboBox1
    //
    this.comboBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.comboBox1.Location = new System.Drawing.Point(368, 456);
    this.comboBox1.Name = "comboBox1";
    this.comboBox1.Size = new System.Drawing.Size(184, 24);
    this.comboBox1.TabIndex = 4;
    this.comboBox1.Text = "comboBox1";
    this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(570, 496);
    this.Controls.Add(this.comboBox1);
    this.Controls.Add(this.btnClose);
    this.Controls.Add(this.btnPiPei);
    this.Controls.Add(this.groupBox2);
    this.Controls.Add(this.groupBox1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.Name = "Form1";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "正则表达式练习工具 V1.0 BY THUNDERS STUDIO";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.groupBox2.ResumeLayout(false);
    this.ResumeLayout(false);

   }
   #endregion

   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
   }

   private void btnClose_Click(object sender, System.EventArgs e)
   {
    this.Close();
   }
   private void Regix(string RegixContect,string MainStr)
   {
    al.Clear();
    al1.Clear();
    Regex r;
    Match m;
    //返回汉字
    r = new Regex(RegixContect,
     RegexOptions.IgnoreCase|RegexOptions.Compiled);
    for (m = r.Match(MainStr); m.Success; m = m.NextMatch())
    {
     if(m.Groups[0].Value.Trim() !="")
         al.Add(m.Groups[0].Value);
     if(m.Groups[1].Value.Trim() !="")
      al1.Add(m.Groups[1].Value);
    }
   }
   private void btnPiPei_Click(object sender, System.EventArgs e)
   {
    Hashtable ht;
    try
    {
     tbResult.Text = "";
     tbResult1.Text = "";
     Regix(tbRegixContent.Text.Trim(),tbContents.Text.Trim());
     if(al.Count ==0 || al1.Count ==0)
      MessageBox.Show("匹配错误!");
     else
     {
      for(int i=0;i<al.Count;i++)
      {
       tbResult.AppendText(al[i].ToString() + "\r\n");
      }
      tbResult.Refresh();
      for(int i=0;i<al1.Count;i++)
      {
       tbResult1.AppendText(al1[i].ToString() + "\r\n");
      }
      tbResult.Refresh();
      tbResult1.Refresh();
     }

    }
    catch
    {
     MessageBox.Show("匹配错误!");
    }
   }

   private void Form1_Load(object sender, System.EventArgs e)
   {
    ht.Add("脚本","(S|s)(r|R)(C|c)(|\\s*)=(|\\s*)(\"|\'|)(?:(?<1>\\S+)(\"|\')|(?<1>\\S+))");
    ht.Add("超链","(h|H)(r|R)(e|E)(f|F)(|\\s*)=(|\\s*)(\"|\'|)(?:(?<1>\\S+)(\"|\')|(?<1>\\S+))");
    ht.Add("邮件地址","(\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*)");
    ht.Add("图片链接","(S|s)(r|R)(C|c)(|\\s*)=(|\\s*)(\"|\'|)(?:(?<1>\\S+)(\"|\')|(?<1>\\S+))");
    ht.Add("ip地址","(\\d+).(\\d+).(\\d+).(\\d+)");
    ht.Add("中国手机号码","\\(86\\)*0*13\\d{9}");
    ht.Add("中国固定电话号码","(\\d{3,4}(-| )\\d{7,8})");
    ht.Add("包括移动和固定电话","((\\d{3,4})|\\d{3,4}-|\\s)?\\d{7,14}");
    ht.Add("邮政编码","[0-9]{1}(\\d+){5}");
    ht.Add("中国身份证号码","(\\d{18})|(\\d{15})");
    ht.Add("整数","(\\d+)");
    ht.Add("浮点数","(\\d+.\\d+)");
    ht.Add("任何数字","(-?\\d*)(.\\d+)?");
    ht.Add("中文字符串","([\\u4e00-\\u9fa5]*)");
    ht.Add("英文字符串","\\w*");
    ht.Add("双字节字符串","([^\\x00-\\xff]*)");
//    comboBox1.DataBindings.Add("SelectedText",ht,"");
//    comboBox1.DataBindings.Add("SelectedValue",ht,"Values");
    string[] array = new string[ht.Count];
    ht.Keys.CopyTo(array,0);
    comboBox1.Text = "脚本";
    comboBox1.Items.AddRange(array);
   
   
   }

   private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
   {
    tbRegixContent.Text = ht[comboBox1.SelectedItem.ToString()].ToString();
   }
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值