/// <summary> /// Initializes a new instance of the <see cref="T:Duplicati.Library.Utility.FilterExpression.FilterEntry"/> struct. /// </summary> /// <param name="filter">The filter string to use.</param> public FilterEntry(string filter) { if (string.IsNullOrEmpty(filter)) { this.Type = FilterType.Empty; this.Filter = null; this.Regexp = null; } else if (filter.StartsWith("[", StringComparison.Ordinal) && filter.EndsWith("]", StringComparison.Ordinal)) { this.Type = FilterType.Regexp; this.Filter = filter.Substring(1, filter.Length - 2); this.Regexp = new Regex(this.Filter, REGEXP_OPTIONS); } else if (filter.StartsWith("{", StringComparison.Ordinal) && filter.EndsWith("}", StringComparison.Ordinal)) { this.Type = FilterType.Group; this.Filter = filter.Substring(1, filter.Length - 2); this.Regexp = GetFilterGroupRegex(this.Filter); } else if (filter.StartsWith("@", StringComparison.Ordinal)) { // Take filter literally; i.e., don't treat wildcard // characters as globbing characters this.Type = FilterType.Simple; this.Filter = filter.Substring(1); this.Regexp = new Regex(Utility.ConvertLiteralToRegExp(this.Filter), REGEXP_OPTIONS); } else { this.Type = (filter.Contains(MULTIPLE_WILDCARD) || filter.Contains(SINGLE_WILDCARD)) ? FilterType.Wildcard : FilterType.Simple; this.Filter = (!Utility.IsFSCaseSensitive && this.Type == FilterType.Wildcard) ? filter.ToUpper(CultureInfo.InvariantCulture) : filter; this.Regexp = new Regex(Utility.ConvertGlobbingToRegExp(filter), REGEXP_OPTIONS); } }
/// <summary> /// Gets the regex that represents the given filter group /// </summary> /// <param name="filterGroupName">Filter group name</param> /// <returns>Group regex</returns> private static Regex GetFilterGroupRegex(string filterGroupName) { FilterGroup filterGroup = FilterGroups.ParseFilterList(filterGroupName, FilterGroup.None); Regex result; if (FilterEntry.filterGroupRegexCache.TryGetValue(filterGroup, out result)) { return(result); } else { // Get the filter strings for this filter group, and convert them to their regex forms List <string> regexStrings = FilterGroups.GetFilterStrings(filterGroup) .Select(filterString => { if (filterString.StartsWith("[", StringComparison.Ordinal) && filterString.EndsWith("]", StringComparison.Ordinal)) { return(filterString.Substring(1, filterString.Length - 2)); } else if (filterString.StartsWith("@", StringComparison.Ordinal)) { return(Utility.ConvertLiteralToRegExp(filterString.Substring(1))); } else { return(Utility.ConvertGlobbingToRegExp(filterString)); } }) .ToList(); string regexString; if (regexStrings.Count == 1) { regexString = regexStrings.Single(); } else { // If there are multiple regex strings, then they need to be merged by wrapping each in parenthesis and ORing them together regexString = "(" + string.Join(")|(", regexStrings) + ")"; } result = new Regex(regexString, REGEXP_OPTIONS); FilterEntry.filterGroupRegexCache[filterGroup] = result; return(result); } }