//
public static Regex WildCardToRegex(string wildCard) {
if (wildCard == null) {
throw new ArgumentNullException("wildCard");
}
if (wildCard == "") {
throw new ArgumentException("wildCard");
}
CheckInvalidPathChars(wildCard); // throws ArgumentException();
StringBuilder sb = new StringBuilder();
var splitResult = wildCard.Split(Win32Path.PathSeparator);
Debug.Assert(splitResult.Length > 0);
foreach (var separated in splitResult) {
var temp = separated;
temp = Regex.Replace(temp, @"([$^\|.{}\[\]()+\\])", @"\$1");
temp = Regex.Replace(temp, @"\*", @".*");
temp = Regex.Replace(temp, @"\?", @".");
sb.AppendFormat("|(?:{0})", temp);
}
sb.Remove(0, 1);
string pattern = @"\\(" + sb.ToString() + ")$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme