@@ -255,32 +255,48 @@ private FindResults FindNameHelper(string packageName, string[] tags, bool inclu
255255 FindResults findResponse = new FindResults ( ) ;
256256 errRecord = null ;
257257
258- WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .*", WildcardOptions . IgnoreCase ) ;
259258 NuGetVersion latestVersion = new NuGetVersion ( "0.0.0.0" ) ;
260259 String latestVersionPath = String . Empty ;
261260 string actualPkgName = packageName ;
262261
262+ // this regex pattern matches packageName followed by a version (4 digit or 3 with prerelease word)
263+ string regexPattern = $ "{ packageName } " + @".\d+\.\d+\.\d+(?:-\w+|.\d)*.nupkg" ;
264+ Regex rx = new Regex ( regexPattern , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
265+ _cmdletPassedIn . WriteDebug ( $ "package file name pattern to be searched for is: { regexPattern } ") ;
266+
263267 foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
264268 {
265269 string packageFullName = Path . GetFileName ( path ) ;
270+ MatchCollection matches = rx . Matches ( packageFullName ) ;
271+ if ( matches . Count == 0 )
272+ {
273+ continue ;
274+ }
266275
267- if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
276+ Match match = matches [ 0 ] ;
277+
278+ GroupCollection groups = match . Groups ;
279+ if ( groups . Count == 0 )
268280 {
269- NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
270- _cmdletPassedIn . WriteDebug ( $ "Version parsed as ' { nugetVersion } '" ) ;
281+ continue ;
282+ }
271283
272- if ( errRecord != null )
273- {
274- return findResponse ;
275- }
284+ Capture group = groups [ 0 ] ;
276285
277- if ( ( ! nugetVersion . IsPrerelease || includePrerelease ) && ( nugetVersion > latestVersion ) )
286+ NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
287+ _cmdletPassedIn . WriteDebug ( $ "Version parsed as '{ nugetVersion } '") ;
288+
289+ if ( errRecord != null )
290+ {
291+ return findResponse ;
292+ }
293+
294+ if ( ( ! nugetVersion . IsPrerelease || includePrerelease ) && ( nugetVersion > latestVersion ) )
295+ {
296+ if ( nugetVersion > latestVersion )
278297 {
279- if ( nugetVersion > latestVersion )
280- {
281- latestVersion = nugetVersion ;
282- latestVersionPath = path ;
283- }
298+ latestVersion = nugetVersion ;
299+ latestVersionPath = path ;
284300 }
285301 }
286302 }
@@ -371,29 +387,46 @@ private FindResults FindVersionHelper(string packageName, string version, string
371387 return findResponse ;
372388 }
373389
374- WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .*", WildcardOptions . IgnoreCase ) ;
390+ // this regex pattern matches packageName followed by the requested version
391+ string regexPattern = $ "{ packageName } .{ requiredVersion . ToNormalizedString ( ) } " + @".nupkg" ;
392+ Regex rx = new Regex ( regexPattern , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
393+ _cmdletPassedIn . WriteDebug ( $ "pattern is: { regexPattern } ") ;
375394 string pkgPath = String . Empty ;
376395 string actualPkgName = String . Empty ;
396+
377397 foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
378398 {
379399 string packageFullName = Path . GetFileName ( path ) ;
380- if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
400+ MatchCollection matches = rx . Matches ( packageFullName ) ;
401+ if ( matches . Count == 0 )
381402 {
382- NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
383- _cmdletPassedIn . WriteDebug ( $ "' { packageName } ' version parsed as ' { nugetVersion } '" ) ;
403+ continue ;
404+ }
384405
385- if ( errRecord != null )
386- {
387- return findResponse ;
388- }
406+ Match match = matches [ 0 ] ;
389407
390- if ( nugetVersion == requiredVersion )
391- {
392- _cmdletPassedIn . WriteDebug ( "Found matching version" ) ;
393- string pkgFullName = $ "{ actualPkgName } .{ nugetVersion . ToString ( ) } .nupkg";
394- pkgPath = Path . Combine ( Repository . Uri . LocalPath , pkgFullName ) ;
395- break ;
396- }
408+ GroupCollection groups = match . Groups ;
409+ if ( groups . Count == 0 )
410+ {
411+ continue ;
412+ }
413+
414+ Capture group = groups [ 0 ] ;
415+
416+ NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
417+ _cmdletPassedIn . WriteDebug ( $ "Version parsed as '{ nugetVersion } '") ;
418+
419+ if ( errRecord != null )
420+ {
421+ return findResponse ;
422+ }
423+
424+ if ( nugetVersion == requiredVersion )
425+ {
426+ _cmdletPassedIn . WriteDebug ( "Found matching version" ) ;
427+ string pkgFullName = $ "{ actualPkgName } .{ nugetVersion . ToString ( ) } .nupkg";
428+ pkgPath = Path . Combine ( Repository . Uri . LocalPath , pkgFullName ) ;
429+ break ;
397430 }
398431 }
399432
0 commit comments