c# - 使用 IsMatch 时的 REGEX 性能问题
问题描述
我有以下用于执行重定向的正则表达式
string requestedPath = HttpUtility.UrlDecode(this.StripLanguage(currentContext.InputUrl.AbsolutePath));
string requestedPathAndQuery = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedRawUrl = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedUrl =
HttpUtility.UrlDecode(
string.Concat(
currentContext.InputUrl.Scheme,
"://",
currentContext.InputUrl.Host,
requestedRawUrl));
string requestedRawUrlDomainAppended = HttpUtility.UrlDecode(currentContext.InputUrl.AbsoluteUri);
string requestedPathWithCulture = HttpUtility.UrlDecode(currentContext.InputUrl.AbsolutePath);
var finalRequestedURL = string.Empty;
finalRequestedURL = Regex.IsMatch(requestedPathAndQuery,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedPathAndQuery
: Regex.IsMatch(requestedPath,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedPath
: Regex.IsMatch(requestedPathWithCulture,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedPathWithCulture
: Regex.IsMatch(requestedRawUrl,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedRawUrl
: Regex.IsMatch(requestedUrl,matchPattern.Trim(),RegexOptions.IgnoreCase)
? requestedRawUrlDomainAppended
: string.Empty;
matchPattern
变量是 URL 。示例:(.*)/articles/my-article(.*)
应重定向到http://www.google.com
正则表达式工作正常,但是当涉及到大量请求时,我们的 CPU 会达到 100%。
有什么解决方案可以优化上述内容吗?
谢谢
解决方案
我会尝试创建一个实际Regex
变量并重用它。这应该有助于加快速度。我也可能会建议将三元业务更改为常规的 if/else if/else 语句。我认为它会更具可读性(只是个人意见)。
string requestedPath = HttpUtility.UrlDecode(this.StripLanguage(currentContext.InputUrl.AbsolutePath));
string requestedPathAndQuery = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedRawUrl = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedUrl =
HttpUtility.UrlDecode(
string.Concat(
currentContext.InputUrl.Scheme,
"://",
currentContext.InputUrl.Host,
requestedRawUrl));
string requestedRawUrlDomainAppended = HttpUtility.UrlDecode(currentContext.InputUrl.AbsoluteUri);
string requestedPathWithCulture = HttpUtility.UrlDecode(currentContext.InputUrl.AbsolutePath);
var regex = new Regex(matchPattern.Trim(), RegexOptions.IgnoreCase);
var finalRequestedURL = regex.IsMatch(requestedPathAndQuery)
? requestedPathAndQuery
: regex.IsMatch(requestedPath)
? requestedPath
: regex.IsMatch(requestedPathWithCulture)
? requestedPathWithCulture
: regex.IsMatch(requestedRawUrl)
? requestedRawUrl
: regex.IsMatch(requestedUrl)
? requestedRawUrlDomainAppended
: string.Empty;
编辑
正如我在上面的评论中指出的那样,有两个相同的字符串,如果您删除其中一个,可以节省比较。
string requestedPath = HttpUtility.UrlDecode(this.StripLanguage(currentContext.InputUrl.AbsolutePath));
string requestedPathAndQuery = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
// This string is identical to requestPathAndQuery, so I am removing it
// string requestedRawUrl = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedUrl =
HttpUtility.UrlDecode(
string.Concat(
currentContext.InputUrl.Scheme,
"://",
currentContext.InputUrl.Host,
requestedRawUrl));
string requestedRawUrlDomainAppended = HttpUtility.UrlDecode(currentContext.InputUrl.AbsoluteUri);
string requestedPathWithCulture = HttpUtility.UrlDecode(currentContext.InputUrl.AbsolutePath);
var regex = new Regex(matchPattern.Trim(), RegexOptions.IgnoreCase);
var finalRequestedURL = string.Empty;
// You could even add in brackets here to aid readability but this
// helps remove the indententation/nesting that makes the code harder
// to read and follow
if (regex.IsMatch(requestedPathAndQuery)) finalRequestURL = requestedPathAndQuery;
else if(regex.IsMatch(requestedPath)) finalRequestURL = requestedPath;
else if (regex.IsMatch(requestedPathWithCulture)) finalRequestURL = requestedPathWithCulture;
else if (regex.IsMatch(requestedUrl)) finalRequestURL = requestedRawUrlDomainAppended;
推荐阅读
- c# - EF Core MySql 自动增量字段
- python-3.x - 分组后..基于熊猫数据框中的组的列值的总和
- azure - 在 Microsoft Virtual Assistant 和 Skills 之间传递数据
- javascript - 如何使用节点接收数据
- c++ - QTextEdit 中的“选择”和“光标”有什么区别?
- r - 在 R 中绘制数据;错误:未为类型“列表”实现默认方法
- python - Eve + 带有特殊字符的 MongoDB
- mysql - 通过 Matlab 与 MySQL 数据库的操作系统无关连接
- powershell - 无法通过 PowerShell 从 FolderBrowserDialog 创建新文件夹
- reactjs - 从子端进行子组件和父组件之间通信的最佳实践是什么?