UrlPattern Class

Represents a URL pattern that can match URLs based on a convenient pattern syntax.

public sealed class UrlPattern

Remarks

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.

WHATWG URL Pattern Spec MDN - URL Pattern API

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

NameDescription
HasRegExpGroupsGets whether this pattern contains one or more groups which use regular expression matching.
HashGets the normalized hash pattern string.
HostnameGets the normalized hostname pattern string.
PasswordGets the normalized password pattern string.
PathnameGets the normalized pathname pattern string.
PortGets the normalized port pattern string.
ProtocolGets the normalized protocol pattern string.
SearchGets the normalized search pattern string.
UsernameGets the normalized username pattern string.

HasRegExpGroups

UrlPattern.cs

Gets whether this pattern contains one or more groups which use regular expression matching.

public bool HasRegExpGroups { get; }

Gets the normalized hash pattern string.

public string Hash { get; }

Gets the normalized hostname pattern string.

public string Hostname { get; }

Gets the normalized password pattern string.

public string Password { get; }

Gets the normalized pathname pattern string.

public string Pathname { get; }

Gets the normalized port pattern string.

public string Port { get; }

Gets the normalized protocol pattern string.

public string Protocol { get; }

Gets the normalized username pattern string.

public string Username { get; }

Methods

NameDescription
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.cs

Creates a new URLPattern from a URLPatternInit dictionary.

public static UrlPattern Create(UrlPatternInit init)
Parameters
init UrlPatternInit
A dictionary containing patterns for each URL component.
Returns

UrlPattern A new URLPattern.

Create(UrlPatternInit, UrlPatternOptions?)

UrlPattern.cs

Creates a new URLPattern from a URLPatternInit dictionary and options.

public static UrlPattern Create(UrlPatternInit init, UrlPatternOptions? options)
Parameters
init UrlPatternInit
A dictionary containing patterns for each URL component.
options UrlPatternOptions?
Options for pattern matching.
Returns

UrlPattern A new URLPattern.

Remarks

Create(string)

UrlPattern.cs

Creates a new URLPattern from a pattern string.

public static UrlPattern Create(string pattern)
Parameters
pattern string
A pattern string using the URL pattern syntax.
Returns

UrlPattern A new URLPattern.

Remarks

Create(string, UrlPatternOptions?)

UrlPattern.cs

Creates a new URLPattern from a pattern string with options.

public static UrlPattern Create(string pattern, UrlPatternOptions? options)
Parameters
pattern string
A pattern string using the URL pattern syntax.
options UrlPatternOptions?
Options for pattern matching.
Returns

UrlPattern A new URLPattern.

Create(string, string?)

UrlPattern.cs

Creates a new URLPattern from a pattern string and base URL.

public static UrlPattern Create(string pattern, string? baseUrl)
Parameters
pattern string
A pattern string using the URL pattern syntax.
baseUrl string?
The base URL to use for relative patterns.
Returns

UrlPattern A new URLPattern.

Create(string, string?, UrlPatternOptions?)

UrlPattern.cs

Creates a new URLPattern from a pattern string, base URL, and options.

public static UrlPattern Create(string pattern, string? baseUrl, UrlPatternOptions? options)
Parameters
pattern string
A pattern string using the URL pattern syntax.
baseUrl string?
The base URL to use for relative patterns.
options UrlPatternOptions?
Options for pattern matching.
Returns

UrlPattern A new URLPattern.

Remarks

IsMatch overloads

IsMatch(UrlPatternInit)

UrlPattern.cs

Indicates whether the pattern finds a match in the specified URL input.

public bool IsMatch(UrlPatternInit input)
Parameters
input UrlPatternInit
The URL input dictionary to test.
Returns

bool true if the pattern matches the input; otherwise, false.

IsMatch(string)

UrlPattern.cs

Indicates whether the pattern finds a match in the specified URL.

public bool IsMatch(string url)
Parameters
url string
The URL string to test.
Returns

bool true if the pattern matches the URL; otherwise, false.

Remarks

IsMatch(string, string?)

UrlPattern.cs

Indicates whether the pattern finds a match in the specified URL with a base URL.

public bool IsMatch(string url, string? baseUrl)
Parameters
url string
The URL string to test.
baseUrl string?
The base URL to use for resolving relative URLs.
Returns

bool true if the pattern matches the URL; otherwise, false.

IsMatch(Uri)

UrlPattern.cs

Indicates whether the pattern finds a match in the specified URL.

public bool IsMatch(Uri url)
Parameters
url Uri
The URL to test.
Returns

bool true if the pattern matches the URL; otherwise, false.

Remarks

A relative URL never matches.

Match overloads

Match(UrlPatternInit)

UrlPattern.cs

Searches 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
input UrlPatternInit
The URL input dictionary to match.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.

Match(string)

UrlPattern.cs

Searches the specified URL for the first occurrence of the pattern and returns the match result with captured groups.

public UrlPatternResult? Match(string url)
Parameters
url string
The URL string to match.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.

Remarks

Match(string, string?)

UrlPattern.cs

Searches 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
url string
The URL string to match.
baseUrl string?
The base URL to use for resolving relative URLs.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.

Match(Uri)

UrlPattern.cs

Searches the specified URL for the first occurrence of the pattern and returns the match result with captured groups.

public UrlPatternResult? Match(Uri url)
Parameters
url Uri
The URL to match.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no match.

Remarks

A relative URL never matches.