From b4b4e606703fd55758093c8d645573203867ebd5 Mon Sep 17 00:00:00 2001 From: Chunqing Chen Date: Tue, 13 Jun 2017 05:48:30 +0800 Subject: [PATCH] Use wildcard matching in help file search (#3971) --- .../help/MUIFileSearcher.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/help/MUIFileSearcher.cs b/src/System.Management.Automation/help/MUIFileSearcher.cs index e5cb5fbe83..c39aaec247 100644 --- a/src/System.Management.Automation/help/MUIFileSearcher.cs +++ b/src/System.Management.Automation/help/MUIFileSearcher.cs @@ -122,9 +122,9 @@ namespace System.Management.Automation 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 @@ namespace System.Management.Automation 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); } }