首页 > 解决方案 > Regex that matches with main route and filters but not matches with sub-routes

问题描述

I have to make a Regular Expression on C# that matches with the following expressions:

/videos/

/videos?filter=some%filter

/videos

But I need that this Regex does not match with expressions like:

/videos/1

/videos/67

For the moment I have the next code, but I do not really know how to complete the expression to make these matches with every word except numbers.

String currentUrl = Context.Request.Path.ToString() + Context.Request.QueryString.ToString();
String pageName = ViewContext.RouteData.Values["controller"].ToString();
Regex reg = new Regex(@"/(videos)(\/)?([^0-9])/g");

if (pageName == "Home")
{
    Html.RenderPartial("Partials/_LandingNavbarPartial");
}
else if (reg.IsMatch((currentUrl.ToLower())))
{
    Html.RenderPartial("Partials/_FilterNavbarPartial");
    @("Matches")
}
else
{
    Html.RenderPartial("Partials/_NavbarPartial");
    @("Not Matches")
}

I am interested in finish my regex expression: /(videos)(\/)?([^0-9])/

标签: c#regexasp.net-corerazor

解决方案


在从头开始学习 Regex 之后,我找到了下一个表达式:

(videos)\/*[^\d]*

正如我提到的那样工作,我想要。


推荐阅读