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); } } } } 希望通过上述案例能帮助大家解决项目实施过程中因写不出匹配模式而产生的烦恼。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
C#中,星号(*)是正则表达式中的特殊字符之一。星号表示匹配前面的元素零次或多次。在正则表达式中,可以通过在模式中使用星号来匹配任意数量的重复字符或子模式。 例如,如果我们有一个字符串"abbbbcccdd",我们可以使用正则表达式"ab*c"来匹配这个字符串中的"abbbbccc"部分。在这个表达式中,星号表示匹配前面的字符'b'零次或多次。因此,它会匹配任意数量的'b'字符。 以下是一个使用星号的C#正则表达式示例: ``` using System; using System.Text.RegularExpressions; public class Program { public static void Main() { string input = "abbbbcccdd"; string pattern = "ab*c"; Regex rgx = new Regex(pattern); Match match = rgx.Match(input); if (match.Success) { Console.WriteLine("匹配成功!"); } else { Console.WriteLine("匹配失败!"); } } } ``` 在这个示例中,正则表达式"ab*c"匹配一个以'a'开头,以'c'结尾,中间可以有任意数量的'b'字符的字符串。因此,它会输出"匹配成功!"。 请注意,为了在C#中使用正则表达式,我们需要引入命名空间System.Text.RegularExpressions,并使用Regex类来实例化正则表达式对象。然后,我们可以使用Match方法来检查输入字符串是否与模式匹配。 希望以上信息对您有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C# 正则表达式替换身份证号中间部分为星号](https://blog.csdn.net/weixin_40508362/article/details/108054301)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [几个C#常用正则表达式的总结](https://download.csdn.net/download/weixin_38667207/14008486)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值