【问题标题】:How to extract an url from a String in C#如何从 C# 中的字符串中提取 url
【发布时间】:2017-01-18 10:38:50
【问题描述】:

我有这个字符串:

 "<figure><img
 src='http://myphotos.net/image.ashx?type=2&image=Images\\2\\9\\11\\12\\3\\8\\4\\7\\685621455625.jpg'
 href='JavaScript:void(0);' onclick='return takeImg(this)'
 tabindex='1' class='myclass' width='55' height='66' alt=\"myalt\"></figure>"

我怎样才能找回这个链接:

http://myphotos.net/image.ashx?type=2&image=Images\\2\\9\\11\\12\\3\\8\\4\\7\\685621455625.jpg

所有字符串都是相同的类型,所以我需要在 src=href 之间获取子字符串。但我不知道该怎么做。谢谢。

【问题讨论】:

标签: c#


【解决方案1】:

如果您解析 HTML,请不要使用字符串方法,而是使用真正的 HTML 解析器,例如 HtmlAgilityPack:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);  // html is your string
var linksAndImages = doc.DocumentNode.SelectNodes("//a/@href | //img/@src");
var allSrcList = linksAndImages
    .Select(node => node.GetAttributeValue("src", "[src not found]"))
    .ToList();

【讨论】:

  • 这是否会从 html 页面中提取所有 URL
  • 它确实从给定htmllinksAndImages 中提取网址
【解决方案2】:

你可以使用正则表达式:

var src = Regex.Match("the string", "<img.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase).Groups[1].Value;

【讨论】:

  • 使用此方法需要删除断线。 Check this
  • @m.rogalski 你这是什么意思?我无法理解
  • 我的意思是,如果您的字符串包含有问题的换行符,则此方法将毫无用处。您可以查看我发布的这个点击链接。,这应该在答案中指定。
【解决方案3】:

一般来说,在解析 HTML 代码中的值时应该使用 HTML/XML 解析器,但是像这样有限的字符串,Regex 就可以了。

string url = Regex.Match(htmlString, @"src='(.*?)'").Groups[1].Value;

【讨论】:

    【解决方案4】:

    如果您的字符串始终采用相同的格式,您可以轻松地这样做:

    string input =  "<figure><img src='http://myphotos.net/image.ashx?type=2&image=Images\\2\\9\\11\\12\\3\\8\\4\\7\\685621455625.jpg' href='JavaScript:void(0);' onclick='return takeImg(this)' tabindex='1' class='myclass' width='55' height='66' alt=\"myalt\"></figure>";
    // link is between ' signs starting from the first ' sign so you can do :
    input = input.Substring(input.IndexOf("'")).Substring(input.IndexOf("'"));
    // now your string looks like : "http://myphotos.net/image.ashx?type=2&image=Images\\2\\9\\11\\12\\3\\8\\4\\7\\685621455625.jpg"
    
    return input;
    

    【讨论】:

      【解决方案5】:
      string str = "<figure><imgsrc = 'http://myphotos.net/image.ashx?type=2&image=Images\\2\\9\\11\\12\\3\\8\\4\\7\\685621455625.jpg'href = 'JavaScript:void(0);' onclick = 'return takeImg(this)'tabindex = '1' class='myclass' width='55' height='66' alt=\"myalt\"></figure>";
      
      int pFrom = str.IndexOf("src = '") + "src = '".Length;
      int pTo = str.LastIndexOf("'href");
      
      string url = str.Substring(pFrom, pTo - pFrom);
      

      来源:

      Get string between two strings in a string

      【讨论】:

        【解决方案6】:

        在这种情况下,Q 是您的字符串,我查找您想要的属性的索引 (src = '),然后删除前几个字符(包括空格在内的 7 个字符),然后您通过查找来查找文本何时结束为'。

        删除前几个字符后,您可以使用 .IndexOf 查找要删除的字符数,因此它不是硬编码的。

                string q =
                    "<figure><img src = 'http://myphotos.net/image.ashx?type=2&image=Images\\2\\9\\11\\12\\3\\8\\4\\7\\685621455625.jpg' href = 'JavaScript:void(0);' onclick = 'return takeImg(this)'" +
                    "tabindex = '1' class='myclass' width='55' height='66' alt=\"myalt\"></figure>";
                string z = q.Substring(q.IndexOf("src = '"));
                z = z.Substring(7);
                z = z.Substring(0, z.IndexOf("'"));
                MessageBox.Show(z);
        

        这当然不是最优雅的方式(查看其他答案:))。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-14
          • 1970-01-01
          • 2015-02-08
          • 1970-01-01
          • 1970-01-01
          • 2011-05-22
          相关资源
          最近更新 更多