Skip to content

Commit

Permalink
Update regex
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Oct 31, 2024
1 parent 859d8f3 commit a4e0c57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
14 changes: 3 additions & 11 deletions src/Aspire.Dashboard/ConsoleLogs/UrlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,9 @@ static void AppendNonMatchFragment(StringBuilder stringBuilder, Func<string, str
}

// Regular expression that detects http/https URLs in a log entry
// Based on the RegEx used in Windows Terminal for the same purpose. Some modifications:
// - Can start at a non word boundary. This behavior is similar to how GitHub matches URLs in pretty printed code.
// - Limited to only http/https URLs.
// - Ignore case. That means it matches URLs starting with http and HTTP.
//
// Explanation:
// https?:// - http:// or https://
// [-A-Za-z0-9+&@#/%?=~_|$!:,.;]* - Any character in the list, matched zero or more times.
// [A-Za-z0-9+&@#/%=~_|$] - Any character in the list, matched exactly once
// Based on the RegEx used by GitHub to detect links in content.
[GeneratedRegex(
"https?://[-A-Za-z0-9+&@#/%?=~_|$!:,.;]*[A-Za-z0-9+&@#/%=~_|$]",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
@"((?<!\+)https?:\/\/(?:www\.)?(?:[-\p{L}.]+?[.@][a-zA-Z\d]{2,}|localhost)(?:[-\w\p{L}.:%+~#*$!?&/=@]*(?:,(?!\s))*?)*)",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture)]
public static partial Regex GenerateUrlRegEx();
}
15 changes: 14 additions & 1 deletion tests/Aspire.Dashboard.Tests/ConsoleLogsTests/UrlParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void TryParse_ReturnsCorrectResult(string input, bool expectedResult, str
[InlineData("http://bing.com/", "<a target=\"_blank\" href=\"http://bing.com/\">http://bing.com/</a>")]
[InlineData("http://bing.com/dir", "<a target=\"_blank\" href=\"http://bing.com/dir\">http://bing.com/dir</a>")]
[InlineData("http://bing.com/index.aspx", "<a target=\"_blank\" href=\"http://bing.com/index.aspx\">http://bing.com/index.aspx</a>")]
[InlineData("http://bing", "<a target=\"_blank\" href=\"http://bing\">http://bing</a>")]
[InlineData("http://localhost", "<a target=\"_blank\" href=\"http://localhost\">http://localhost</a>")]
public void TryParse_SupportedUrlFormats(string input, string? expectedOutput)
{
var result = UrlParser.TryParse(input, WebUtility.HtmlEncode, out var modifiedText);
Expand Down Expand Up @@ -83,4 +83,17 @@ public void GenerateUrlRegEx_MatchUrlAfterContent(string content)
var match = regex.Match(content);
Assert.Equal("http://www.localhost:8080", match.Value.ToLowerInvariant());
}

[Theory]
[InlineData("http://www.localhost:8080!", "http://www.localhost:8080!")]
[InlineData("http://www.localhost:8080/path!", "http://www.localhost:8080/path!")]
[InlineData("http://www.localhost:8080/path;", "http://www.localhost:8080/path")]
[InlineData("http://www.localhost:8080;", "http://www.localhost:8080")]
[InlineData("http://www.local;host:8080;", "http://www.local")]
public void GenerateUrlRegEx_MatchUrlBeforeContent(string content, string expected)
{
var regex = UrlParser.GenerateUrlRegEx();
var match = regex.Match(content);
Assert.Equal(expected, match.Value.ToLowerInvariant());
}
}

0 comments on commit a4e0c57

Please sign in to comment.