-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathWildcard.cs
37 lines (35 loc) · 1.44 KB
/
Wildcard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace Microsoft.Win32.TaskScheduler
{
/// <summary>
/// Represents a wildcard running on the
/// <see cref="System.Text.RegularExpressions"/> engine.
/// </summary>
public class Wildcard : Regex
{
/// <summary>
/// Initializes a wildcard with the given search pattern and options.
/// </summary>
/// <param name="pattern">The wildcard pattern to match.</param>
/// <param name="options">A combination of one or more <see cref="System.Text.RegularExpressions.RegexOptions"/>.</param>
public Wildcard([NotNull] string pattern, RegexOptions options = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace)
: base(WildcardToRegex(pattern), options)
{
}
/// <summary>
/// Converts a wildcard to a regular expression.
/// </summary>
/// <param name="pattern">The wildcard pattern to convert.</param>
/// <returns>A regular expression equivalent of the given wildcard.</returns>
public static string WildcardToRegex([NotNull] string pattern)
{
string s = Regex.Escape(pattern);
s = Regex.Replace(s, @"(?<!\\)\\\*", @".*"); // Negative Lookbehind
s = Regex.Replace(s, @"\\\\\\\*", @"\*");
s = Regex.Replace(s, @"(?<!\\)\\\?", @"."); // Negative Lookbehind
s = Regex.Replace(s, @"\\\\\\\?", @"\?");
return string.Concat("^", Regex.Replace(s, @"\\\\\\\\", @"\\"), "$");
}
}
}