public static void PrintSchema(StringPattern pattern) { Console.WriteLine($"Pattern: \"{pattern.Pattern}\"\n\n----- Tokens -----\n"); foreach (Token item in pattern.Tokens) { Console.WriteLine($"{item.Type.ToString().ToUpper()}: \"{item.Content}\""); } }
public static bool Match(string input, StringPattern pattern, bool debug = false) { if (debug) { Console.WriteLine($"Pattern: {pattern.Pattern}"); Console.WriteLine($"Input: {input}\n"); } for (int i = 0; i < pattern.tokens.Count; i++) { Token item = pattern.tokens[i]; if (debug) { Console.WriteLine($"-> \"{input}\""); } switch (item.Type) { case TokenType.Required: if (!input.StartsWith(item.Content)) { return(false); } input = input.Substring(item.Content.Length); break; case TokenType.Optional: if (input.StartsWith(item.Content)) { input = input.Substring(item.Content.Length); } break; case TokenType.AnyCnTkn: if (i < pattern.tokens.Count - 1) { Token next = null; for (int j = i + 1; j < pattern.tokens.Count; j++) { next = pattern.tokens[j]; if (pattern.tokens[j].Type != TokenType.AnyCnTkn) { break; } } if (next != null) { int startIndex = input.IndexOf(next.Content); if (startIndex >= 0) { input = input.Substring(startIndex); } } } else { input = string.Empty; } break; case TokenType.SngleTkn: if (input.Length == 0) { return(false); } input = input.Substring(1); break; default: throw new NotImplementedException($"The token \"{item.Type} ({item.Content})\" currently is not implemented!"); } } bool match = input.Length == 0; if (debug) { Console.WriteLine($"IsMatch: " + match + "\n--------------------------------------------------"); } return(match); }
public static bool IsMatch(string pattern, string input) { StringPattern sp = Compile(pattern); return(sp.IsMatch(input)); }