using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace CSharpUtil.Text { /// /// 正则表达式工具类,集合了一些常见表达式. 内建了表达式缓存. 避免每次使用重新编译. /// author:gwang /// public class RegHelper { #region ' 构造函数 ' private RegHelper() { } static Dictionary cache = new Dictionary(); static Regex GetRegex(string name, string patrn, RegexOptions opts = RegexOptions.None) { if (!cache.ContainsKey(name)) { cache.Add(name, new Regex(patrn, opts)); } return cache[name]; } #endregion #region ' 方法 ' /// /// 校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串 /// /// /// static public bool isRegisterUserName(string s) { const string rname = "RegisterUserName"; const string patrn = @"^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$"; Regex re = GetRegex(rname, patrn); return re.IsMatch(s); } /// /// 校验玩家昵称: 只能输入5到10个汉字或者数字、英文字母 /// /// /// static public bool isNickName(string s) { const string rname = "NickName"; const string patrn = @"^[a-zA-Z0-9_\u4e00-\u9fa5]{4,9}$"; Regex re = GetRegex(rname, patrn); return re.IsMatch(s); } /// /// 电话号码-固机(010/0311-1234567(8)) /// /// /// static public bool isTellNumber(string s) { const string rname = "TellNumber"; const string patrn = @"^((d{3,4})|d{3,4}-)?d{7,8}$"; Regex re = GetRegex(rname, patrn); return re.IsMatch(s); } /// /// 电话号码-手机号(国内) /// /// /// static public bool isCellPhoneNumber(string s) { const string rname = "TellNumber"; const string patrn = @"^0?(13|14|15|18)[0-9]{9}$"; Regex re = GetRegex(rname, patrn); return re.IsMatch(s); } #endregion } }