/// <summary> /// Metoda vrátí true, pokud daný pattern je formálně správný a může být použit v metodě <see cref="CreateWildcardsRegex(string)"/>. /// </summary> /// <param name="pattern">Pattern s užitím standardních Wildcards * a ?</param> /// <returns></returns> public static bool IsWildcardsValid(string pattern) { if (pattern == null) { return(false); } pattern = pattern.Trim(); if (pattern.Length == 0) { return(false); } if (IllegalCharactersRegex.IsMatch(pattern)) { return(false); } return(true); }
/// <summary> /// Metoda vrátí <see cref="Regex"/>, který dovoluje porovnávat texty se standardní Wildcards notací. /// Tedy: text "Abcdefg" vyhovuje patternu "Ab??e*". /// Volitelně lze požádat, aby <see cref="Regex"/> měl zapnutou option <see cref="RegexOptions.IgnoreCase"/>: true = ignoruje velikost znaků, false = neignoruje /// </summary> /// <param name="pattern">Pattern s užitím standardních Wildcards * a ?</param> /// <param name="ignoreCase">true = ignoruje velikost znaků, false = neignoruje</param> /// <returns></returns> public static Regex CreateWildcardsRegex(string pattern, bool ignoreCase) { if (pattern == null) { throw new ArgumentNullException(); } pattern = pattern.Trim(); if (pattern.Length == 0) { throw new ArgumentException("Pattern is empty."); } if (IllegalCharactersRegex.IsMatch(pattern)) { throw new ArgumentException("Pattern contains illegal characters."); } bool hasExtension = CatchExtentionRegex.IsMatch(pattern); bool matchExact = false; if (HasQuestionMarkRegEx.IsMatch(pattern)) { matchExact = true; } else if (hasExtension) { matchExact = CatchExtentionRegex.Match(pattern).Groups[1].Length != 3; } string regexString = Regex.Escape(pattern); regexString = "^" + Regex.Replace(regexString, @"\\\*", ".*"); regexString = Regex.Replace(regexString, @"\\\?", "."); if (!matchExact && hasExtension) { regexString += NonDotCharacters; } regexString += "$"; RegexOptions regexOptions = (ignoreCase ? RegexOptions.Compiled | RegexOptions.IgnoreCase : RegexOptions.Compiled); Regex regex = new Regex(regexString, regexOptions); return(regex); }