C#獲取字符中指定標籤的屬性值和標籤的內容值

/// <summary>  
        /// 獲取字符中指定標籤的屬性值  
        /// </summary>  
        /// <param name="str">字符串</param>  
        /// <param name="tagName">標籤</param>  
        /// <param name="attrib">屬性名</param>  
        /// <returns>屬性</returns>  
        public static List<string> GetTagAttr(string str, string tagName, string attrib)
        {
            string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", tagName, attrib);
            //獲取<Script>屬性值  url

            MatchCollection titleMatch = Regex.Matches(str, tmpStr, RegexOptions.IgnoreCase);orm

            List<string> list = new List<string>();
            foreach (Match m in titleMatch)
            {
                string result = m.Groups["url"].Value;
                if (string.IsNullOrEmpty(result) || list.Contains(result)) continue;ip

                list.Add(result);
            }字符串

            return list;
        }string

        /// <summary>  
        /// 獲取字符中指定標籤的值  
        /// </summary>  
        /// <param name="str">字符串</param>  
        /// <param name="tagName">標籤</param>  
        /// <returns>值</returns>  
        public static List<string> GetTagContent(string str, string tagName)
        {
            string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", tagName, tagName); //獲取<Script>之間內容  it

            MatchCollection titleMatch = Regex.Matches(str, tmpStr, RegexOptions.IgnoreCase);io

            List<string> list = new List<string>();
            foreach (Match m in titleMatch)
            {
                string result = m.Groups["Text"].Value;
                if (string.IsNullOrEmpty(result) || list.Contains(result)) continue;foreach

                list.Add(result);
            }
            return list;
        }
 List

相關文章
相關標籤/搜索