TheAlgorithms/C-Sharp · error · ArgumentException
Pattern cannot start with *
Error message
Pattern cannot start with *
What it means
WildCardMatcher.MatchPattern throws ArgumentException when the pattern's first character is '*', because a leading '*' with nothing to anchor on is treated as invalid in this DP formulation. The matcher supports '*' and '?' wildcards but requires the pattern to start with a concrete character.
Solutions
- Rewrite the pattern so it does not begin with '*', e.g. "*abc" is rejected but "a*bc" is accepted; for match-anything prefixes consider prefixing with an empty-matchable character per the library's conventions.
- Validate/sanitize user-supplied patterns before calling and reject or transform leading '*' cases.
- Catch ArgumentException and return a clear validation message to the user.
- If full glob semantics are needed, use a matcher that permits leading wildcards.
Example fix
// before
WildCardMatcher.MatchPattern(fileName, "*.txt"); // throws
// after
var pattern = fileNamePattern.StartsWith("*") ? fileNamePattern : fileNamePattern; // strip/normalize leading '*'
WildCardMatcher.MatchPattern(fileName, ".txt"); // or use a glob library for '*.txt' Defensive patterns
Strategy: validation
Validate before calling
if (pattern.Length > 0 && pattern[0] == '*')
{
// reject or transform the pattern before calling MatchPattern
throw new FormatException("Pattern must not start with '*'.");
} Try / catch
try
{
matched = WildCardMatcher.MatchPattern(input, pattern);
}
catch (ArgumentException ex) when (ex.Message.Contains("Pattern cannot start with"))
{
// report invalid pattern to the user
} Prevention
- Sanitize user-supplied wildcards: reject or rewrite patterns beginning with '*'.
- Document the accepted pattern grammar ('?' and embedded '*') for users.
- Show pattern syntax help in UIs where patterns are entered.
- Add validation tests for edge patterns like "*", "*a", "a*".
When it happens
Trigger: Calling MatchPattern(input, "*abc") or MatchPattern(input, "*") — any pattern whose index-0 character is '*'.
Common situations: Users typing glob-style patterns like "*.txt" that start with a wildcard; patterns copied from shell glob or regex semantics where leading '*' is valid; configuration files storing patterns authored by end users.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- The value for some n_i is smaller than or equal to 1.
- The GCD of n_ = and n_ = equals and thus these values…
- The pattern is longer than 31 characters.
- Strings must be equal length.
- Invalid parameter settings for Ascon Hash
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/22f68c02caef9aff.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Strings/PatternMatching/WildCardMatcher.cs:26
/// </summary>
public static class WildCardMatcher
{
/// <summary>
/// Using bottom-up dynamic programming for matching the input string with the pattern.
///
/// Time complexity: O(n*m), where n is the length of the input string and m is the length of the pattern.
///
/// Constrain: The pattern cannot start with '*'.
/// </summary>
/// <param name="inputString">The input string to match.</param>
/// <param name="pattern">The pattern to match.</param>
/// <returns>True if the input string matches the pattern, false otherwise.</returns>
/// <exception cref="ArgumentException">Thrown when the pattern starts with '*'.</exception>
public static bool MatchPattern(string inputString, string pattern)
{
if (pattern.Length > 0 && pattern[0] == '*')
{
throw new ArgumentException("Pattern cannot start with *");
}
var inputLength = inputString.Length + 1;
var patternLength = pattern.Length + 1;
// DP 2d matrix, where dp[i, j] is true if the first i characters in the input string match the first j characters in the pattern
// This DP is initialized to all falses, as it is the default value for a boolean.
var dp = new bool[inputLength, patternLength];
// Empty string and empty pattern are a match
dp[0, 0] = true;
// Since the empty string can only match a pattern that has a * in it, we need to initialize the first row of the DP matrix
for (var j = 1; j < patternLength; j++)
{
if (pattern[j - 1] == '*')
{
dp[0, j] = dp[0, j - 2];View on GitHub (pinned to 96e2905cab)