Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text.RegularExpressions;
- class AdBlocker
- {
- private Regex adRegex;
- public AdBlocker()
- {
- // Load the list of ad domains from a file
- LoadAdDomains("ad_domains.txt");
- }
- public void LoadAdDomains(string filePath)
- {
- try
- {
- // Read the file containing ad domains
- string[] domains = File.ReadAllLines(filePath);
- // Create a regular expression pattern to match ad domains
- string pattern = string.Format(@"(^|\.)({0})$", string.Join("|", domains));
- // Compile the regular expression
- adRegex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Failed to load ad domains: " + ex.Message);
- }
- }
- public bool IsAd(string url)
- {
- // Check if the URL matches the ad domain pattern
- return adRegex.IsMatch(url);
- }
- }
- class Program
- {
- static void Main()
- {
- // Create an instance of the AdBlocker
- AdBlocker adBlocker = new AdBlocker();
- // Test URLs
- string[] urls = {
- "https://www.example.com",
- "https://ads.example.com",
- "https://www.advertisement.com",
- "https://www.example.org"
- };
- // Check if each URL is an ad
- foreach (string url in urls)
- {
- bool isAd = adBlocker.IsAd(url);
- Console.WriteLine("{0}: {1}", url, isAd ? "Blocked" : "Allowed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement