c#实用工具方法——贰

        #region 处理动态Key的json对象

        /// <summary>
        /// 本类用于处理动态Key的json对象
        /// </summary>
        /// <param name="jObject">需要处理的json对象</param>
        /// <param name="strJson"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns>json对象的第一个元素的values</returns>
        public static string GetJsonValue(string strJson, out string[] name, out string[] value)
        {
            var jo = JObject.Parse(strJson);
            var values = jo.Properties().Select(item => item.Value.ToString()).ToArray();
            var keys = jo.Properties().Select(item => item.Name.ToString()).ToArray();
            name = keys;
            value = values;
            var strResult = values[0];
            return strResult;
        }
        #endregion

        #region HTML字符串处理
        /// <summary>      
        /// 返回匹配多个的集合值      
        /// </summary>      
        /// <param name="start">开始html tag</param>      
        /// <param name="end">结束html tag</param>      
        /// <param name="html">html</param>      
        ///      

        public static IList<string> GetHtmls(string start, string end, string html)
        {
            IList<string> list = new List<string>();
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[\r\n])+?){1}", start, end);//匹配URL的模式,并分组      
                MatchCollection mc = Regex.Matches(html, pattern);//满足pattern的匹配集合      
                if (mc.Count != 0)
                {
                    foreach (Match match in mc)
                    {
                        GroupCollection gc = match.Groups;
                        list.Add(gc["g"].Value);
                    }
                }
            }
            catch { }
            return list;
        }

        public static string GetHtml(string start, string end, string html)
        {
            string ret = string.Empty;
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[\r\n])+?)?{1}", start, end);//匹配URL的模式,并分组      
                ret = Regex.Match(html, pattern).Groups["g"].Value;

            }
            catch { }

            return ret;
        }
        public static string funcGetSrc(string aText)
        {
            string pattern = @"(?is)<img (?:title=(['""]?)?(?<title>[^'""]*?)\1\s*|src=(['""]?)?(?<url>[^'""]*?)\2\s*)*/>";
            Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection mc = r.Matches(aText);
            string imgname = "";
            foreach (Match m in mc)
            {
                imgname += m.Groups[1].Value;
            }
            return imgname;
        }

        public static List<string> Get_A_Href(string html)
        {
            List<String> links = new List<String>();

            //MatchCollection matches = Regex.Matches(html, "<a(?:\\s+.+?)*?\\s+href=\"([^\"]*?)\".+>(.*?)</a>", RegexOptions.IgnoreCase);
            MatchCollection matches = Regex.Matches(html, @"(?is)<a[^>]*?href=(['""\s]?)(?<href>[^'""\s]*)\1[^>]*?>", RegexOptions.IgnoreCase);
            foreach (Match match in matches)
            {
                string s = match.Groups[1].Value;
                links.Add(s);
            }
            return links;
        }

        /// <summary>     
        /// 获取字符中指定标签的值     
        /// </summary>     
        /// <param name="str">字符串</param>     
        /// <param name="title">标签</param>     
        /// <returns>值</returns>     
        public static List<string> GetTitleContent(string str, string title)
        {
            string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title); //获取<title>之间内容     

            MatchCollection TitleMatchs = Regex.Matches(str, tmpStr, RegexOptions.IgnoreCase);
            List<string> list = new List<string>();
            if (TitleMatchs.Count != 0)
            {
                foreach (Match TitleMatch in TitleMatchs)
                {
                    string result = TitleMatch.Groups["Text"].Value;
                    list.Add(result);
                }
            }
            return list;
        }

        /// <summary>     
        /// 获取字符中指定标签的值     
        /// </summary>     
        /// <param name="str">字符串</param>     
        /// <param name="title">标签</param>     
        /// <param name="attrib">属性名</param>     
        /// <returns>属性</returns>     
        public static List<string> GetTitleContent(string str, string title, string attrib)
        {

            string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", title, attrib); //获取<title>之间内容     

            MatchCollection TitleMatchs = Regex.Matches(str, tmpStr, RegexOptions.IgnoreCase);
            List<string> list = new List<string>();
            if (TitleMatchs.Count != 0)
            {
                foreach (Match TitleMatch in TitleMatchs)
                {
                    string result = TitleMatch.Groups["url"].Value;
                    list.Add(result);
                }
            }
            return list;
        }
        #endregion

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值