zoukankan      html  css  js  c++  java
  • c#正则表达式获取html超链接

     private Dictionary<string, string> GetUrl(string content)
            { 
                Dictionary<string, string> dics = new Dictionary<string, string>();
    
                string pattern = @"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>";
                MatchCollection mc = Regex.Matches(content, pattern);
                foreach (Match m in mc)
                {
                    if (m.Success)
                    {
                        //加入集合数组
                        //hrefList.Add(m.Groups["href"].Value);
                        //nameList.Add(m.Groups["name"].Value);
                        try
                        {
                            dics.Add(m.Groups["url"].Value, m.Groups["text"].Value);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                return dics;
            }
  • 相关阅读:
    C++ 日期 & 时间
    C++ 引用
    C++ 指针
    C++ 字符串
    C++ 数组
    C++ 数字
    C++ 函数
    C++ 判断
    C++ 循环
    C++ 运算符
  • 原文地址:https://www.cnblogs.com/yannis/p/2916995.html
Copyright © 2011-2022 走看看