As @Mike said, you should use a library which would shorten your code and parse the html more precisely.
In case you have a specific purpose, I will provide you with two ways to demonstrate how to get the hyperlink which only have episode and number hyperlink.
One is using Regex. You have to use regular expression language.
Another one is using HtmlAgilityPack which supports plain XPATH or XSLT to find the node from html. XPATH is user-friendly as you can refer to below code.
static void Main(string[] args)
{
Console.WriteLine("Results from Regex:");
GetLinkByRegex();
Console.WriteLine("Results from HtmlAgilityPack:");
GetLinkByAgilityPack();
Console.ReadKey();
}
Demo:
Hope this can help you.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
131 Points
731 Posts
Get number of episode using Regex
Mar 06, 2020 02:27 PM|pamyral_279|LINK
Hello all,
I have a content with html which look like this :
<div>....</div>
<a href="https://a.com/movies/monkeys-season-1?episode=1" class="btn btn-default btn-episode">1</a>
<a href="https://a.com/movies/monkeys-season-1?episode=2" class="btn btn-default btn-episode">2</a>
<a href="https://a.com/movies/monkeys-season-1?episode=3" class="btn btn-default btn-episode">3</a>
<a href="https://a.com/movies/monkeys-season-1?episode=4" class="btn btn-default btn-episode">4</a>
.....
<a href="https://a.com/movies/monkeys-season-1?episode=20" class="btn btn-default btn-episode">20</a>
<p>...</p>
<a href="def.com">...</a>
<div>...</div>
My Question :
How can i use Regex to get hyperlink which only have episode and number hyperlink ?
I use C# language.
Thank you so much !
All-Star
194506 Points
28081 Posts
Moderator
Re: Get number of episode using Regex
Mar 06, 2020 04:47 PM|Mikesdotnetting|LINK
If you want to parse HTML, you should use a library designed for that instead of Regex. Try AngleSharp: https://github.com/AngleSharp/AngleSharp
Contributor
2900 Points
852 Posts
Re: Get number of episode using Regex
Mar 09, 2020 04:10 AM|Sean Fang|LINK
Hi, pamyral_279,
As @Mike said, you should use a library which would shorten your code and parse the html more precisely.
In case you have a specific purpose, I will provide you with two ways to demonstrate how to get the hyperlink which only have episode and number hyperlink.
One is using Regex. You have to use regular expression language.
Another one is using HtmlAgilityPack which supports plain XPATH or XSLT to find the node from html. XPATH is user-friendly as you can refer to below code.
1.Using Regex
Code:
2. Using HtmlAgilityPack
Code:
Main method:
Demo:
Hope this can help you.
Best regards,
Sean