zoukankan      html  css  js  c++  java
  • 掩码转换为正则表达式

       bool MatchCheck(string sn,string pattern)
            {
                return Regex.IsMatch(sn, pattern, RegexOptions.None);
            }
            
            /// <summary>
            ///  掩码转换为正则表达式
            /// </summary>
            /// <param name="msk">掩码 CTH********F2GV**</param>
            /// <param name="token">掩码符号* #</param>
            /// <returns>regexp表达式</returns>
            public static string MaskToRegex(string msk,string token)
            {
                if (string.IsNullOrEmpty(msk) || string.IsNullOrEmpty(token))
                    return string.Empty;
                MatchCollection mac = Regex.Matches(msk, string.Format(@"\b(\w+)|([{0}]+)|\b",token));
                string regex = string.Empty;
                foreach (Match x in mac)
                {
                    if (!string.IsNullOrEmpty(x.Value))
                    {
                        if (Regex.IsMatch(x.Value, @"\w+"))
                        {
                            regex += x.Value;                        
                        }
                        if (Regex.IsMatch(x.Value, string.Format(@"[{0}]+",token)))
                        {
                            string tempmsk = string.Format(@"\w{0}", "{" + x.Value.Length + "}");                        
                            regex += tempmsk;
                        }
                    }
                }
                
                string pattern = string.Format("^{0}$", regex);
                return pattern;
            }
    

      

  • 相关阅读:
    2014年广州区域赛k题解
    2014年广州区域赛e题解
    2014年广州区域赛i题解
    最大化平均值问题
    codeforces 976e 题解
    maven
    机器学习入门
    拟合
    插值
    熵权法
  • 原文地址:https://www.cnblogs.com/senion/p/regex.html
Copyright © 2011-2022 走看看