C#正则表达式获取属性

        /// <summary>
        /// 获取属性
        /// </summary>
        /// <param name="str">配置字符串</param>
        /// <param name="title">title</param>
        /// <param name="attribute">属性名称</param>
        /// <returns>值  缺省返回string.empty</returns>
        public static string GetAttribute(string str, string title, string attribute)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(attribute)) return string.Empty;
            if (title == null) title = string.Empty;
            string regexStr = string.Format("<{0}[^>]*? {1}[\\s]*?=[\\s]*?(['\"\"]?)(?<value>[^'\"\"\\s>]+)\\1[^>]*>", title, attribute); //获取<title>之间内容  
            Match TitleMatch = Regex.Match(str, regexStr, RegexOptions.IgnoreCase);
            return TitleMatch.Groups["value"].Value;
        }

 

posted @ 2016-09-01 09:37  杯酒不言欢  阅读(295)  评论(0编辑  收藏  举报