{"record":{"id":"22f68c02caef9aff","repo":"TheAlgorithms/C-Sharp","slug":"pattern-cannot-start-with","errorCode":null,"errorMessage":"Pattern cannot start with *","messagePattern":"Pattern cannot start with \\*","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Strings/PatternMatching/WildCardMatcher.cs","lineNumber":26,"sourceCode":"/// </summary>\npublic static class WildCardMatcher\n{\n    /// <summary>\n    ///    Using bottom-up dynamic programming for matching the input string with the pattern.\n    ///\n    ///    Time complexity: O(n*m), where n is the length of the input string and m is the length of the pattern.\n    ///\n    ///    Constrain: The pattern cannot start with '*'.\n    /// </summary>\n    /// <param name=\"inputString\">The input string to match.</param>\n    /// <param name=\"pattern\">The pattern to match.</param>\n    /// <returns>True if the input string matches the pattern, false otherwise.</returns>\n    /// <exception cref=\"ArgumentException\">Thrown when the pattern starts with '*'.</exception>\n    public static bool MatchPattern(string inputString, string pattern)\n    {\n        if (pattern.Length > 0 && pattern[0] == '*')\n        {\n            throw new ArgumentException(\"Pattern cannot start with *\");\n        }\n\n        var inputLength = inputString.Length + 1;\n        var patternLength = pattern.Length + 1;\n\n        // 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\n        // This DP is initialized to all falses, as it is the default value for a boolean.\n        var dp = new bool[inputLength, patternLength];\n\n        // Empty string and empty pattern are a match\n        dp[0, 0] = true;\n\n        // 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\n        for (var j = 1; j < patternLength; j++)\n        {\n            if (pattern[j - 1] == '*')\n            {\n                dp[0, j] = dp[0, j - 2];","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Strings/PatternMatching/WildCardMatcher.cs#L8-L44","documentation":"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.","triggerScenarios":"Calling MatchPattern(input, \"*abc\") or MatchPattern(input, \"*\") — any pattern whose index-0 character is '*'.","commonSituations":"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.","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."],"exampleFix":"// before\nWildCardMatcher.MatchPattern(fileName, \"*.txt\"); // throws\n// after\nvar pattern = fileNamePattern.StartsWith(\"*\") ? fileNamePattern : fileNamePattern; // strip/normalize leading '*'\nWildCardMatcher.MatchPattern(fileName, \".txt\"); // or use a glob library for '*.txt'","handlingStrategy":"validation","validationCode":"if (pattern.Length > 0 && pattern[0] == '*')\n{\n    // reject or transform the pattern before calling MatchPattern\n    throw new FormatException(\"Pattern must not start with '*'.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    matched = WildCardMatcher.MatchPattern(input, pattern);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Pattern cannot start with\"))\n{\n    // report invalid pattern to the user\n}","preventionTips":["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*\"."],"tags":["invalid-argument-value","wildcard","pattern-matching","csharp"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}