从 html 链接中提取标题 [英] Extract Title from html link

查看:56
本文介绍了从 html 链接中提取标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 HTML 字符串:

I have the following HTML string:

<a href="/tothepage" title="the page">The Link</a>.  

如何轻松地从 HTML 片段中提取标题?首选正则表达式或其他 VB.NET 解决方案,但 C# 也可以.

How can I extract title from the HTML snippet with ease? Either a regex or other VB.NET solution is preferred but C# is ok.

我想要页面"而不是链接文本:我想要标题属性的值.

I want 'the page' not the link text: I want the value of the title attribute.

我已经安装了 HTMLAgilityPack,如果这样做很容易的话.

I have HTMLAgilityPack installed if it's easy to do it with that.

推荐答案

使用正则表达式,组将包含它([^"]*):

With a regular expression, the group will contain it ([^"]*):

title="([^"]*)"

C#

using System.Text.RegularExpressions;
static void Main(string[] args)
    {
        string originalString = "<a href=\" / tothepage\" title=\"the page\">The Link</a>.";
        Regex rgx = new Regex("title=\"([^\"]*)\"", RegexOptions.IgnoreCase);
        Match match = rgx.Matches(originalString)[0];
        Console.WriteLine(match.Groups[1]);
        Console.ReadLine();
    }

这篇关于从 html 链接中提取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆