- 浏览: 47535 次
- 性别:
- 来自: 青岛
最新评论
很多的学员听了正则表达式的讲座后,对于匹配模式的应用不太灵活或不知如何下手写匹配模式,个人做了一次归纳,列出大部分项目设计中需要的常用匹配模式,大致如下:
窗体设计代码: 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); } } } } 希望通过上述案例能帮助大家解决项目实施过程中因写不出匹配模式而产生的烦恼。
发表评论
-
java 字符串数组 操作
2012-07-06 09:51 841public class StringTool { ... -
如何用正则表达式表示不包含2950的字符串?转
2012-07-06 09:45 825使用Excel中的VBA测试如下(匹配式为:^((?!29 ... -
MoonShell2皮肤文件(.skn) 结构解析
2012-07-06 09:30 714首先感谢Moonlight的 ... -
js 写的可多选的日历控件
2012-07-05 20:45 1119郑重申明, 此日历非本人作品 作者: 张剑锋(我同 ... -
alt 进程外com组件的连接事件
2012-07-03 13:43 11251. 问题的提出 类似于设计模式中Observer模式 ... -
flex 利用饼图生成内部环状图
2012-07-02 10:40 710width="800" ... -
flex 缩略图处理方式
2012-07-02 10:40 693一种是: 如果DisplayObject对象是存在a ... -
Flex基于FXG仪表盘(附全部源代码)二
2012-07-02 10:40 566上文我们已经完成一个仪表盘的fxg,然后把我们的fxg转化 ... -
Flex右键菜单扫盲
2012-07-02 10:40 622怎么添加右键?在 Flex 中,只有应用程序中的顶层组件才 ... -
如何使用registerClassAlias来解决module中使用RemoteObject---第一次访问正常、第二次出错
2012-07-01 00:17 569问题:今天我在Flex的module中使用RemoteOb ... -
Silverlight关于容器与子控件的加载与事件触发
2012-07-01 00:17 609Silverlight中的控件在加载时,会先触发子控件的L ... -
windows下制作PHP扩展
2012-07-01 00:17 697转自:http://demon.tw/software/c ... -
java面笔试总结
2012-07-01 00:16 5012010、10、12 湖南华诺集团面试题集 1、D ...
相关推荐
C#常用正则表达式,如:用户名 [\w\-]{3,12} 3-12个字符(包括英文字母、数字、'-'、'_')
c#常用正则表达式. c#常用正则表达式. c#常用正则表达式.
里面集合了比较常用的正则表达式,例:利用正则表达式限制网页表单里的文本框输入内容、匹配URL、允许用户的输入限制等等..
C#中的常用正则表达式总结 C#中的常用正则表达式总结 C#中的常用正则表达式总结
c#中常用的一些正则表达式。c#中常用的一些正则表达式。
c#常用正则,c#常用正则,c#常用正则,c#常用正则
包含了常用正则表达式的使用,验证,正则表达式替换字符串,判断字符串是否为正整数,判断输入的字符串是否全是英文、中文....
C# 中的常用正则表达式总结C# 中的常用正则表达式总结
C# 中的常用正则表达式总结 常用的正则表达式
c#正则表达式常用,验证数字位数,是否为数字,是否为字符,验证字符数个数,验证年龄的填写规范等。
c#常用正则表达式,c#常用正则表达式,c#常用正则表达式
常用的正则表达式 C#正则表达式小结 - z 只能输入数字:"^[0-9]*$"。 只能输入n位的数字:"^\d{n}$"。 只能输入至少n位的数字:"^\d{n,}$"。 只能输入m~n位的数字:。"^\d{m,n}$" 只能输入零和非零开头的数字:"^(0|...
常用正则表达式整理收集常用正则表达式整理收集常用正则表达式整理收集
C#验证类 使用正则表达式包含多种验证表达式的写作方法与注释
《正则表达式经典实例》讲解了基于8种常用的编程语言使用正则表达式的经典实例。书中提供了上百种可以在实战中使用的实例,以帮助读者使用正则表达式来处理数据和文本。对于如何使用正则表达式来解决性能不佳、误报...
一些常用的C#正则表达式,包含E-mail地址等等!
常用验证正则表达式,手机号验证,身份证号验证,邮箱验证,等等,
只能输入汉字:"^[\一-\龥]{0,}$" 验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。 ...%&=]*)?$"。 验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX...
C#中常用到正则表达式总结(比较全面),有需要的来学习学习