Alston Antony
Alston Antony

Reputation: 179

Google Search Engine Position Regex

Currently my regex in C# is

string lookup = "(<h3 class=\"r\"><a href=\"/url?q=)(\\w+[a-zA-Z0-9.\\-?=/:]*)";

I am trying to match google search engine position result which looks like below

<h3 class="r"><a href="/url?q=https://uk.yahoo.com/&amp;sa=U&amp;ved=0CBUQFjAAahUKEwi_koaWptjHAhXG8HIKHYcQCl0&amp;usg=AFQjCNHkhixstCvIO45GIKI44_kp6ul36w">

This code function more detail

string lookup = "(<h3 class=\"r\"><a href=\"/url?q=)(\\w+[a-zA-Z0-9.\\-?=/:]*)";
            MatchCollection matches = Regex.Matches(html, lookup);

            for (int i = 0; i < matches.Count; i++)
            {
                string match = matches[i].Groups[2].Value;
                if (match.Contains(url.Host))
                    return i + 1;
            }

            return 0;

the error which I am experiencing is I always getting "0"

Upvotes: 1

Views: 755

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627100

Here is an option with HtmlAgilityPack HTML parser (install it using Manage NuGet Packages for Solution from the drop-down menu when right-clicking the solution name in the Solution explorer):

var html = "<h3 class=\"r\"><a href=\"/url?q=https://uk.yahoo.com/&amp;sa=U&amp;ved=0CBUQFjAAahUKEwi_koaWptjHAhXG8HIKHYcQCl0&amp;usg=AFQjCNHkhixstCvIO45GIKI44_kp6ul36w\">";
var tags_with_attributes = new List<KeyValuePair<string, List<KeyValuePair<string, string>>>>();
var kvp = new KeyValuePair<string, List<KeyValuePair<string, string>>>();
HtmlAgilityPack.HtmlDocument hap;
Uri uriResult;
if (Uri.TryCreate(html, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp)
{ // html is a URL 
   var doc = new HtmlAgilityPack.HtmlWeb();
   hap = doc.Load(uriResult.AbsoluteUri);
}
else
{ // html is a string
   hap = new HtmlAgilityPack.HtmlDocument();
   hap.LoadHtml(html);
}
var nodes = hap.DocumentNode.SelectNodes("//h3/a[@href]");
if (nodes != null)
{
   foreach (var node in nodes)
   {
       foreach (var attribute in node.Attributes)
          if (attribute.Name == "href" && attribute.Value.StartsWith("/url?q="))
            Console.WriteLine(attribute.Value.Substring(7));
    }
}

This will parse Web page if use an URL in html, or a HTML string.

Upvotes: 0

MaticDiba
MaticDiba

Reputation: 905

Problem with the regex above is that you forgot to escape ?

string lookup = "(<h3 class=\"r\"><a href=\"/url\\?q=)(\\w+[a-zA-Z0-9.\\-?=/:]*)";

I'm not sure if the regex returns what you want, because I wasn't able to understand what you are trying to match from your description. But for sure unexcaped "?" is a problem.

Upvotes: 3

Related Questions