| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Utilities; |
| | | 4 | | |
| | | 5 | | internal static class RegexUtils |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Checks if the input string matches the glob pattern. |
| | | 9 | | /// The pattern can contain '*' for any sequence of characters and '?' for a single character. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <param name="input">The input string to match against the pattern.</param> |
| | | 12 | | /// <param name="pattern">The glob pattern to match.</param> |
| | | 13 | | /// <param name="ignoreCase">Whether to ignore case when matching.</param> |
| | | 14 | | /// <returns>True if the input matches the pattern, otherwise false.</returns> |
| | | 15 | | public static bool IsGlobMatch(string input, string pattern, bool ignoreCase = true) |
| | | 16 | | { |
| | | 17 | | // Escape regex metacharacters then bring back * and ? |
| | 12 | 18 | | var re = "^" + Regex.Escape(pattern) |
| | 12 | 19 | | .Replace(@"\*", ".*") |
| | 12 | 20 | | .Replace(@"\?", ".") + "$"; |
| | | 21 | | |
| | 12 | 22 | | var flags = ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None; |
| | 12 | 23 | | return Regex.IsMatch(input, re, flags); |
| | | 24 | | } |
| | | 25 | | } |