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); } } } } 希望通过上述案例能帮助大家解决项目实施过程中因写不出匹配模式而产生的烦恼。
资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 在苹果的生态系统中,IAP(应用内购买)是苹果应用商店(App Store)中应用开发者常采用的一种盈利模式,允许用户在应用内直接购买虚拟商品或服务。苹果为开发者提供了一份详细的人民币(CNY)IAP定价表,这份定价表具有以下特点: 价格分级:定价表由多个价格等级组成,开发者可根据虚拟商品的价值选择相应等级,等级越高,价格越高。例如,低等级可能对应基础功能解锁,高等级则对应高级服务或大量虚拟道具。 税收与分成:苹果会从应用内购买金额中抽取30%作为服务费或佣金,这是苹果生态的固定规则。不过,开发者实际到手的收入会因不同国家和地区的税收政策而有所变化,但定价表中的价格等级本身是固定的,便于开发者统一管理。 多级定价策略:通过设置不同价格等级,开发者可以根据商品或服务的类型与价值进行合理定价,以满足不同消费能力的用户需求,从而最大化应用的总收入。例如,一款游戏可以通过设置不同等级的虚拟货币包,吸引不同付费意愿的玩家。 特殊等级:除了标准等级外,定价表还包含备用等级和特殊等级(如备用等级A、备用等级B等),这些等级可能是为应对特殊情况或促销活动而设置的额外价格点,为开发者提供了更灵活的定价选择。 苹果IAP定价表是开发者设计应用内购机制的重要参考。它不仅为开发者提供了标准的收入分成模型,还允许开发者根据产品特性设定价格等级,以适应市场和满足不同用户需求。同时,开发者在使用定价表时,还需严格遵守苹果的《App Store审查指南》,包括30%的分成政策、使用苹果支付接口、提供清晰的产品描述和定价信息等。苹果对应用内交易有严格规定,以确保交易的透明性和安全性。总之,苹果IAP定价表是开发者在应用内购设计中不可或缺的工具,但开发者也需密切关注苹果政策变化,以确保应用的合规运营和收益最大化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值