`
hgfghwq19
  • 浏览: 47535 次
  • 性别: Icon_minigender_2
  • 来自: 青岛
社区版块
存档分类
最新评论

C# 常用正则表达式

 
阅读更多

  很多的学员听了正则表达式的讲座后,对于匹配模式的应用不太灵活或不知如何下手写匹配模式,个人做了一次归纳,列出大部分项目设计中需要的常用匹配模式,大致如下:
  窗体设计代码:       namespace RegexDemo { partial class FrmRegex { ///  /// 必需的设计器变量。 ///  private System.ComponentModel.IContainer components = null; ///  /// 清理所有正在使用的资源。 ///  /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 ///  /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 ///  private void InitializeComponent() { this.lblCheckContent = new System.Windows.Forms.Label(); this.btnCheck = new System.Windows.Forms.Button(); this.txtCheckContent = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // lblCheckContent // this.lblCheckContent.AutoSize = true; this.lblCheckContent.Location = new System.Drawing.Point(12, 36); this.lblCheckContent.Name = "lblCheckContent"; this.lblCheckContent.Size = new System.Drawing.Size(77, 14); this.lblCheckContent.TabIndex = 2; this.lblCheckContent.Text = "检测内容:"; // // btnCheck // this.btnCheck.Location = new System.Drawing.Point(212, 88); this.btnCheck.Name = "btnCheck"; this.btnCheck.Size = new System.Drawing.Size(75, 27); this.btnCheck.TabIndex = 1; this.btnCheck.Text = "检测(&C)"; this.btnCheck.UseVisualStyleBackColor = true; this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click); // // txtCheckContent // this.txtCheckContent.Location = new System.Drawing.Point(95, 33); this.txtCheckContent.Name = "txtCheckContent"; this.txtCheckContent.Size = new System.Drawing.Size(192, 23); this.txtCheckContent.TabIndex = 0; this.txtCheckContent.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt CheckContent_KeyPress); // // FrmRegex // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(299, 127); this.Controls.Add(this.txtCheckContent); this.Controls.Add(this.btnCheck); this.Controls.Add(this.lblCheckContent); this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "FrmRegex"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n; this.Text = "正则表达式案例"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label lblCheckContent; private System.Windows.Forms.Button btnCheck; private System.Windows.Forms.TextBox txtCheckContent; } }        后置代码:       using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; /*System.Text.RegularExpressions。该名称空间包括8个类,1个枚举,1个委托。他们分别是: * Capture: 包含一次匹配的结果; * CaptureCollection: Capture的序列; * Group: 一次组记录的结果,由Capture继承而来; * GroupCollection:表示捕获组的集合 * Match: 一次表达式的匹配结果,由Group继承而来; * MatchCollection: Match的一个序列; * MatchEvaluator: 执行替换操作时使用的委托; * Regex:编译后的表达式的实例。 * RegexCompilationInfo:提供编译器用于将正则表达式编译为独立程序集的信息 * RegexOptions 提供用于设置正则表达式的枚举值 * */ /*Regex类中还包含一些静态的方法: * Escape: 对字符串中的regex中的转义符进行转义; * IsMatch: 如果表达式在字符串中匹配,该方法返回一个布尔值; * Match: 返回Match的实例; * Matches: 返回一系列的Match的方法; * Replace: 用替换字符串替换匹配的表达式; * Split: 返回一系列由表达式决定的字符串; * Unescape:不对字符串中的转义字符转义。 * */ namespace RegexDemo { public partial class FrmRegex : Form { public FrmRegex() { InitializeComponent(); } private void btnCheck_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtCheckContent.Text)) { MessageBox.Show(this, "请填写要检测的内容!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); this.txtCheckContent.Focus(); return; } string sCondition = null; #region 匹配Email //sCondition = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; #endregion #region 匹配日期 //sCondition = @"((1|2)\d{3})(-|\/)(((1)(1|2|0))|((0)(1|2|3|4|5|6 |7|8|9))|(1|2|3|4|5|6|7|8|9))(-|\/)(((1|2)(1|2|3|4| 5|6|7|8|9|0))|((0)(1|2|3|4|5|6|7|8|9))|((3)(1|0))|( 1|2|3|4|5|6|7|8|9))"; #endregion #region 匹配中文 //sCondition = @"^[\u4e00-\u9fa5]{0,}$"; #endregion #region 匹配URL //sCondition = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; #endregion #region 匹配数字(只能输入数字) //sCondition = @"^[0-9]*$"; #endregion #region 匹配数字(只能输入n位数字) //sCondition = @"^\d{5}$"; #endregion #region 匹配数字(只能输入至少n位的数字) //sCondition = @"^\d{5,}$"; #endregion #region 匹配数字(只能输入m~n位的数字) //sCondition = @"^\d{3,8}$"; #endregion #region 匹配数字(只能输入有两位小数的正实数) sCondition = @"^[0-9]+(.[0-9]{2})?$"; #endregion #region 匹配字母(只能输入由26个英文字母组成的字符串) //sCondition = @"^[A-Za-z]+$"; #endregion #region 匹配字母(只能输入由26个大写英文字母组成的字符串) //sCondition = @"^[A-Z]+$"; #endregion #region 匹配字母(只能输入由数字和26个英文字母组成的字符串) //sCondition = @"^[A-Za-z0-9]+$"; #endregion #region 匹配字母(只能输入由数字、26个英文字母或者下划线组成的字符串) //sCondition = @"^\w+$"; #endregion #region 匹配用户密码(以字母开头,长度在6~18之间,只能包含字符、数字和下划线) //sCondition = @"^[a-zA-Z]\w{5,17}$"; #endregion #region 匹配一年的12个月 //sCondition = @"^(0?[1-9]|1[0-2])$"; #endregion #region 匹配一个月中的31天 //Condition = @"^((0?[1-9])|((1|2)[0-9])|30|31)$"; #endregion #region 匹配正整数或正浮点数 //sCondition = @"^[0-9]*$"; //sCondition = @"^\d+(\.\d+)?$"; #endregion if (this.IsMatch(sCondition, this.txtCheckContent.Text)) { MessageBox.Show(this, "匹配成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(this, "匹配失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); this.txtCheckContent.Text = ""; this.txtCheckContent.Focus(); } } ///  /// 利用正则表达式匹配相关内容 ///  /// 匹配条件 /// 要匹配的内容 ///  public bool IsMatch(string matchCondition,string matchContent) { Regex objRegex = new Regex(matchCondition); return objRegex.IsMatch(matchContent); } private void txtCheckContent_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { this.btnCheck_Click(sender, e); } } } }       希望通过上述案例能帮助大家解决项目实施过程中因写不出匹配模式而产生的烦恼。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics