UrlPattern Class
Represents a URL pattern that can match URLs based on a convenient pattern syntax.
public sealed class UrlPatternRemarks
The URL Pattern API provides a web platform primitive for matching URLs based on a convenient pattern syntax.
A URL pattern consists of several components (protocol, hostname, pathname, etc.), each of which represents a pattern that can be matched against the corresponding component of a URL.
Example
// Create a pattern and test if a URL matches
var pattern = UrlPattern.Create("https://example.com/users/:id");
Assert.True(pattern.IsMatch("https://example.com/users/123"));
// Extract captured groups from a URL
var result = pattern.Match("https://example.com/users/456");
Assert.NotNull(result);
Assert.Equal("456", result.Pathname.Groups["id"]);
// Use wildcards to match any value
var wildcardPattern = UrlPattern.Create("https://*.example.com/*");
Assert.True(wildcardPattern.IsMatch("https://api.example.com/data"));Properties
| Name | Description |
|---|---|
| HasRegExpGroups | Gets whether this pattern contains one or more groups which use regular expression matching. |
| Hash | Gets the normalized hash pattern string. |
| Hostname | Gets the normalized hostname pattern string. |
| Password | Gets the normalized password pattern string. |
| Pathname | Gets the normalized pathname pattern string. |
| Port | Gets the normalized port pattern string. |
| Protocol | Gets the normalized protocol pattern string. |
| Search | Gets the normalized search pattern string. |
| Username | Gets the normalized username pattern string. |
HasRegExpGroups
UrlPattern.csGets whether this pattern contains one or more groups which use regular expression matching.
public bool HasRegExpGroups { get; }Hash
UrlPattern.csGets the normalized hash pattern string.
public string Hash { get; }Hostname
UrlPattern.csGets the normalized hostname pattern string.
public string Hostname { get; }Password
UrlPattern.csGets the normalized password pattern string.
public string Password { get; }Pathname
UrlPattern.csGets the normalized pathname pattern string.
public string Pathname { get; }Port
UrlPattern.csGets the normalized port pattern string.
public string Port { get; }Protocol
UrlPattern.csGets the normalized protocol pattern string.
public string Protocol { get; }Search
UrlPattern.csGets the normalized search pattern string.
public string Search { get; }Username
UrlPattern.csGets the normalized username pattern string.
public string Username { get; }Methods
| Name | Description |
|---|---|
| Create(UrlPatternInit) | Creates a new URLPattern from a URLPatternInit dictionary. |
| Create(UrlPatternInit, UrlPatternOptions?) | Creates a new URLPattern from a URLPatternInit dictionary and options. |
| Create(string) | Creates a new URLPattern from a pattern string. |
| Create(string, UrlPatternOptions?) | Creates a new URLPattern from a pattern string with options. |
| Create(string, string?) | Creates a new URLPattern from a pattern string and base URL. |
| Create(string, string?, UrlPatternOptions?) | Creates a new URLPattern from a pattern string, base URL, and options. |
| IsMatch(UrlPatternInit) | Indicates whether the pattern finds a match in the specified URL input. |
| IsMatch(string) | Indicates whether the pattern finds a match in the specified URL. |
| IsMatch(string, string?) | Indicates whether the pattern finds a match in the specified URL with a base URL. |
| IsMatch(Uri) | Indicates whether the pattern finds a match in the specified URL. |
| Match(UrlPatternInit) | Searches the specified URL input for the first occurrence of the pattern and returns the match result with captured groups. |
| Match(string) | Searches the specified URL for the first occurrence of the pattern and returns the match result with captured groups. |
| Match(string, string?) | Searches the specified URL with a base URL for the first occurrence of the pattern and returns the match result with captured groups. |
| Match(Uri) | Searches the specified URL for the first occurrence of the pattern and returns the match result with captured groups. |
Create overloads
Create(UrlPatternInit)
UrlPattern.csCreates a new URLPattern from a URLPatternInit dictionary.
public static UrlPattern Create(UrlPatternInit init)Parameters
initUrlPatternInit- A dictionary containing patterns for each URL component.
Returns
UrlPattern A new URLPattern.
Create(UrlPatternInit, UrlPatternOptions?)
UrlPattern.csCreates a new URLPattern from a URLPatternInit dictionary and options.
public static UrlPattern Create(UrlPatternInit init, UrlPatternOptions? options)Parameters
initUrlPatternInit- A dictionary containing patterns for each URL component.
optionsUrlPatternOptions?- Options for pattern matching.
Returns
UrlPattern A new URLPattern.
Remarks
Create(string)
UrlPattern.csCreates a new URLPattern from a pattern string.
public static UrlPattern Create(string pattern)Parameters
patternstring- A pattern string using the URL pattern syntax.
Returns
UrlPattern A new URLPattern.
Remarks
Create(string, UrlPatternOptions?)
UrlPattern.csCreates a new URLPattern from a pattern string with options.
public static UrlPattern Create(string pattern, UrlPatternOptions? options)Parameters
patternstring- A pattern string using the URL pattern syntax.
optionsUrlPatternOptions?- Options for pattern matching.
Returns
UrlPattern A new URLPattern.
Create(string, string?)
UrlPattern.csCreates a new URLPattern from a pattern string and base URL.
public static UrlPattern Create(string pattern, string? baseUrl)Parameters
patternstring- A pattern string using the URL pattern syntax.
baseUrlstring?- The base URL to use for relative patterns.
Returns
UrlPattern A new URLPattern.
Create(string, string?, UrlPatternOptions?)
UrlPattern.csCreates a new URLPattern from a pattern string, base URL, and options.
public static UrlPattern Create(string pattern, string? baseUrl, UrlPatternOptions? options)Parameters
patternstring- A pattern string using the URL pattern syntax.
baseUrlstring?- The base URL to use for relative patterns.
optionsUrlPatternOptions?- Options for pattern matching.
Returns
UrlPattern A new URLPattern.
Remarks
IsMatch overloads
IsMatch(UrlPatternInit)
UrlPattern.csIndicates whether the pattern finds a match in the specified URL input.
public bool IsMatch(UrlPatternInit input)Parameters
inputUrlPatternInit- The URL input dictionary to test.
Returns
bool true if the pattern matches the input; otherwise, false.
IsMatch(string)
UrlPattern.csIsMatch(string, string?)
UrlPattern.csIndicates whether the pattern finds a match in the specified URL with a base URL.
IsMatch(Uri)
UrlPattern.csMatch overloads
Match(UrlPatternInit)
UrlPattern.csSearches the specified URL input for the first occurrence of the pattern and returns the match result with captured groups.
public UrlPatternResult? Match(UrlPatternInit input)Parameters
inputUrlPatternInit- The URL input dictionary to match.
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.
Match(string)
UrlPattern.csSearches the specified URL for the first occurrence of the pattern and returns the match result with captured groups.
public UrlPatternResult? Match(string url)Parameters
urlstring- The URL string to match.
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.
Remarks
Match(string, string?)
UrlPattern.csSearches the specified URL with a base URL for the first occurrence of the pattern and returns the match result with captured groups.
public UrlPatternResult? Match(string url, string? baseUrl)Parameters
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.
Match(Uri)
UrlPattern.csSearches the specified URL for the first occurrence of the pattern and returns the match result with captured groups.
public UrlPatternResult? Match(Uri url)Parameters
urlUri- The URL to match.
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.
Remarks
A relative URL never matches.