Use generated Regex attribute to generate the expression implementation at compile time.

This commit is contained in:
Ross Healy
2024-02-08 02:39:07 +00:00
parent 10039eff21
commit ea3f91e28d

View File

@@ -7,7 +7,7 @@ using System.Text.RegularExpressions;
namespace lottery_co_uk_scraper.NationalLottery
{
internal class DrawWinnerInformation
internal partial class DrawWinnerInformation
{
public static void ProcessTableSection(HtmlNode table, string sectionTitle, LottoResult lottoResult)
{
@@ -419,7 +419,7 @@ namespace lottery_co_uk_scraper.NationalLottery
var textNodes = htmlDoc.DocumentNode.DescendantsAndSelf().Where(n => n.NodeType == HtmlNodeType.Text);
var numericValues = textNodes.SelectMany(n => Regex.Matches(n.InnerHtml, @"(\d+)").Cast<Match>())
var numericValues = textNodes.SelectMany(n => MyRegex().Matches(n.InnerHtml).Cast<Match>())
.Select(match => int.Parse(match.Groups[1].Value))
.ToList();
@@ -449,7 +449,6 @@ namespace lottery_co_uk_scraper.NationalLottery
string columnTitle = prizePerWinnerNode.Attributes["data-title"].Value;
// Now you can use regularPrizePerWinner and rolldownPrizePerWinner as needed
Console.WriteLine($"Prize per winner ({sectionTitle}): {regularPrizePerWinner}");
Console.WriteLine($"Rolldown Prize per winner ({sectionTitle}): {rolldownPrizePerWinner}");
@@ -531,5 +530,8 @@ namespace lottery_co_uk_scraper.NationalLottery
{
AssignValueToModelProperty(sectionTitle, columnTitle, LotteryTableRow.Total, winnersNode, lottoResult);
}
[GeneratedRegex(@"(\d+)")]
private static partial Regex MyRegex();
}
}