12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace CSharpUtil.Text
- {
- /// <summary>
- /// 正则表达式工具类,集合了一些常见表达式. 内建了表达式缓存. 避免每次使用重新编译.
- /// author:gwang
- /// </summary>
- public class RegHelper
- {
- #region ' 构造函数 '
- private RegHelper()
- {
- }
- static Dictionary<string, Regex> cache = new Dictionary<string, Regex>();
- 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 ' 方法 '
- /// <summary>
- /// 校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 校验玩家昵称: 只能输入5到10个汉字或者数字、英文字母
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 电话号码-固机(010/0311-1234567(8))
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 电话号码-手机号(国内)
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- 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
- }
- }
|