-1

I'm trying to make a redirect tool for a website and I need a dotnet regular expression for an specific route with an specific query string.

I need to match /folder/file.aspx?options=all

I need to match the url part (/folder/file.aspx) as it is and in the query string match if the options=all parameter is there even if there are other parameters.

chapinero
  • 1
  • 1
  • Are you trying to find matching URL within a page, or do you have a URL and are trying to check if it matches the rule? – Rawling Jun 08 '16 at 06:59

1 Answers1

2

Instead of using RegEx for this there is a dedicated URI class in C# that will do this for you as explained here. You can combine it with HttpUtitility to extract the query parameters.

var uri = new Uri("/folder/file.aspx?options=all");
var options = HttpUtility.ParseQueryString(uri.Query).Get("options");

If you prefer Regex for non-obvious reasons, there you go:

var regex = new Regex("/folder/file\.aspx\?(?:\w+(?:=\w+)?&)*options=all");

You can see it live in action on Regex101.

Community
  • 1
  • 1
Toxantron
  • 2,218
  • 12
  • 23