Exemple #1
0
        /// <summary>
        /// 属性格式化
        /// </summary>
        /// <param name="reg"></param>
        /// <param name="strText"></param>
        /// <returns></returns>
        public string Property_Callbak(RegexInfo reg, string strText)
        {
            MatchCollection matchs = reg.r.Matches(strText);

            // 没有匹配的直接返回原值
            if (matchs.Count == 0)
            {
                return(strText);
            }

            // 先将正则出来的结果进行帅选出来进行细化定位(KEY=开始位置)
            List <Match> result = new List <Match>();

            foreach (Match m in matchs)
            {
                result.Add(m);
            }
            // 将位置将序排下顺序,后面的优先处理
            List <Match> sortList = result.OrderByDescending(p => p.Index).ToList();

            StringBuilder builder = new StringBuilder(strText);

            // 将结果中的项目通过指定的函数进行格式化并替换值
            foreach (Match node in sortList)
            {
                if (node.Groups.Count == 0)
                {
                    continue;
                }

                // 获取到键值
                string strKey = node.Groups[1].ToString();
                // 对象容器中没有注册此键值
                if (!m_nodeList.ContainsKey(strKey))
                {
                    continue;
                }

                // 正则出来字符串
                string strOldText = node.Groups[0].ToString();

                PropNode prop       = m_nodeList[strKey];
                string   strNewText = "";
                // 处理值
                if (prop.handler != null)
                {
                    strNewText = prop.handler(node);
                }
                else
                {
                    strNewText = FormatPropertyValue(node, prop);
                }

                // 有变化将进行替换
                if (!strNewText.Equals(strOldText))
                {
                    // 本正则出来字符串在源字符串的起始位置
                    int nStartPos = node.Index;
                    // 正则出来字符串的长度
                    int nTextLength = node.Length;

                    // 替换
                    builder.Replace(strOldText, strNewText, nStartPos, nTextLength);
                }
            }

            return(builder.ToString());
        }