C# 用正则表达式构造重定向URL

C# 用正则表达式构造重定向URL,c#,asp.net,regex,redirect,C#,Asp.net,Regex,Redirect,如何使用Regex重定向示例 example.com/en/solution/platform features example.com/en/platform features如果您的目的是始终删除第三个元素,那么这应该可以做到 var reg = new Regex(@"(?<Begin>([^/]*/){2})[^/]*/(?<End>.*)"); var match = reg.Match(@"exemple.com/en/solution/platform-fe

如何使用Regex重定向示例

example.com/en/solution/platform features


example.com/en/platform features

如果您的目的是始终删除第三个元素,那么这应该可以做到

var reg = new Regex(@"(?<Begin>([^/]*/){2})[^/]*/(?<End>.*)");
var match = reg.Match(@"exemple.com/en/solution/platform-features");
var result = match.Groups["Begin"].Value + match.Groups["End"].Value;
var reg=newregex(@“(?([^/]*/){2})[^/]*/(?*)”;
var match=reg.match(@“example.com/en/solution/platform features”);
var result=match.Groups[“Begin”].Value+match.Groups[“End”].Value;

您不需要正则表达式;这只是一个简单的替换场景。尽管如此,为了使其更灵活,您可以为相关部件添加如下可配置值:

var pattern = ConfigurationManager.AppSettings["pattern"];
var replacement = ConfigurationManager.AppSettings["replacement"];
var url = @"exemple.com/en/solution/platform-features";
var resultUrl = url.Replace(pattern, replacement);
return resultUrl;
在您的配置文件中,只需输入:

<appSettings>
    <add key="pattern" value="/solution/" /> <!--Don't just replace "solution"-it can be another part of the url-->
    <add key="replacement" value="/" />
</appSettings>


这个正则表达式将得到您想要的结果,但无论如何,您都可以在c#

中为/使用split函数并将其重新连接,您也可以不使用正则表达式来实现这一点。你想使用regex从url中删除最后一个目录名吗?我需要使用regex表达式执行301重定向这是我的雇主要求的,是的,我想从我的url中删除/solution/要删除的单词总是“solution”吗?如果是这样的话,你可以不用正则表达式来完成。如果不是,如果格式与示例相同,您仍然可以在不使用正则表达式的情况下执行此操作;tmp[tmp.Length-2]=null;返回string.Join(“/”,tmp.Where(x=>string.IsNullOrWhiteSpace(x)==false))?首先。我相信这是可以改进的。谢谢,但我真的需要使用正则表达式在sitecore中使用别名进行301重定向,我知道这是一个noob问题。我正在为我的新工作开发sitecore功能,但以前从未使用过.NET dev,所以我很挣扎,我需要能够使用此正则表达式字符串在sitecore中使用别名进行301重定向。
/([^/]+)(?=/[^/]+/?$)