diff --git a/src/System.Management.Automation/help/MUIFileSearcher.cs b/src/System.Management.Automation/help/MUIFileSearcher.cs index e5cb5fbe836..c39aaec247c 100644 --- a/src/System.Management.Automation/help/MUIFileSearcher.cs +++ b/src/System.Management.Automation/help/MUIFileSearcher.cs @@ -122,9 +122,9 @@ private string[] GetFiles(string path, string pattern) ArrayList result = new ArrayList(); string[] files = Directory.GetFiles(path); - string regexPattern = pattern.Replace(".", @"\."); - regexPattern = regexPattern.Replace("*", ".*"); - regexPattern = regexPattern.Replace("?", ".?"); + var wildcardPattern = WildcardPattern.ContainsWildcardCharacters(pattern) + ? WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase) + : null; foreach (string filePath in files) { @@ -133,11 +133,12 @@ private string[] GetFiles(string path, string pattern) result.Add(filePath); break; } - // If the input is pattern instead of string, we need to use Regex expression. - if (pattern.Contains("*") || pattern.Contains("?")) - { - if (Regex.IsMatch(filePath, regexPattern)) - { + + if (wildcardPattern != null) + { + string fileName = Path.GetFileName(filePath); + if (wildcardPattern.IsMatch(fileName)) + { result.Add(filePath); } }