-1

I am trying to use variable inside the below regex but I am unable to get the correct output.

   (?<=="Page 2" class="fl" href=")(.*?)(?=\"><span class=\"csb)

-- I tried below but not sure I missed something.

(("?<== \"Page "+ pages++ + "\" class=\"fl\" href=\"")"(.*?)"(?=\"><span class=\"csb"));

--

How I can achieve it?

Edit:

   aria-label="Page 2" class="fl" href="/search?q=.net+regex+tester&amp;ei=p3Q7XNzkBt6RwgPrga-YBQ&amp;start=10&amp;sa=N&amp;ved=0ahUKEwic1OvFo-vfAhXeiHAKHevAC1MQ8tMDCHw"><span class="csb ch"

-- Added the source.

nav
  • 95
  • 1
  • 11
  • 1
    One pair of parentheses should be inside the string at the beginning and at the end. Also, you should be using the [HtmlAgilityPack](https://www.nuget.org/packages/HtmlAgilityPack/) for HTML parsing. Regex is not difficult and error prone (e.g. if attributes are inverted). – Olivier Jacot-Descombes Jan 13 '19 at 18:15
  • It's generally considered [bad practice](https://stackoverflow.com/a/1732454/4415734) to use RegEx to parse a structured language such as HTML as others have mentioned. – Callum Watkins Jan 13 '19 at 19:05

1 Answers1

1

We need to view on this issue not like "how to write correct regex query".

Just look like whole c# regex variable (object).

Your code in c# looks like (added double quotes)

var reg = new Regex(@"(?<==""Page 2"" class=""fl"" href="")(.*?)(?=\""><span class=\""csb)");

So, we need to String.Format the Regex string query, like

var reg = new Regex($@"(?<==""Page {page}"" class=""fl"" href="")(.*?)(?=\""><span class=\""csb)");

Btw, if we are talking about parsing html, it's strongly not recommended to use Regular Expression. More info

dima_horror
  • 3,098
  • 1
  • 20
  • 24