Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to parse package name and version number from nuget package filenames

I have a directory of nuget packages that I've downloaded from nuget.org. I'm trying to create a regex that will parse out the package name and version number from the filename. It doesn't seem difficult at first glance; the filenames have a clear pattern:

{PackageName}.{VersionNumber}.nupkg

Edge cases make it challenging though.

  • Package names can have dashes, underscores, and numbers
  • Package names can have effectively unlimited parts separated by dots
  • Version numbers consist of 3-4 groups of numbers, separated by dots
  • Version numbers sometimes are suffixed with pre-release tags (-alpha, -beta, etc)

Here's a sample list of nuget package filenames:

knockoutjs.3.4.2.nupkg
log4net.2.0.8.nupkg
runtime.tizen.4.0.0-armel.microsoft.netcore.jit.2.0.0.nupkg
nuget.core.2.7.0-alpha.nupkg
microsoft.identitymodel.6.1.7600.16394.nupkg

I want to be able to do a search/replace in a Serious Text Editor where the search is a regex with two groups, one for the package name and one for the version number. The output should be "Package: \1 Version: \2". With the 5 packages above, the output should be:

Package: knockoutjs Version: 3.4.2
Package: log4net Version: 2.0.8
Package: runtime.tizen.4.0.0-armel.microsoft.netcore.jit Version: 2.0.0
Package: nuget.core Version: 2.7.0-alpha
Package: microsoft.identitymodel Version: 6.1.7600.16394

The closest relatively concise regex I've come up with is:

^([^\s]*)\.((?:[0-9]+\.){3,})nupkg$

...which results in the following output:

Package: knockoutjs Version: 3.4.2.
Package: log4net Version: 2.0.8.
Package: runtime.tizen.4.0.0-armel.microsoft.netcore.jit Version: 2.0.0.
nuget.core.2.7.0-alpha.nupkg
Package: microsoft.identitymodel.6 Version: 1.7600.16394.

It handles the first three decently, although I don't want that trailing dot. It doesn't even match on the fourth one, and the fifth one has the first part of the version number lumped in with the package name.

Save the day!

like image 944
BobbyA Avatar asked Aug 02 '18 22:08

BobbyA


People also ask

How do I find the NuGet package version?

In Visual Studio, use the Help > About Microsoft Visual Studio command and look at the version displayed next to NuGet Package Manager. Alternatively, launch the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console) and enter $host to see information about NuGet including the version.

How do I get files from a NuGet package?

on the toolbar of the Assembly Explorer window or choose File | Open from NuGet Packages Cache in the main menu . This will open the Open from NuGet Packages Cache dialog. The dialog lists packages from all NuGet cache locations on your machine. Use the search field in the dialog to find the desired package.

Does NuGet use SemVer?

NuGet 4.3. 0+ supports SemVer 2.0. 0, which supports pre-release numbers with dot notation, as in 1.0.

How do I change NuGet package version?

Right-click the Packages folder in the project, and select Update. This will update the NuGet package to the latest version. You can double-click the Add packages and choose the specific version.


Video Answer


2 Answers

I modified your expression slightly to:

^(.*?)\.((?:\.?[0-9]+){3,}(?:[-a-z]+)?)\.nupkg$

The main points are that I moved the . in front of the digits in the first non capturing group, and that I added an optional non capturing group for -alpha in the fourth string.

Replace with:

Package: \1 Version: \2

Test the regex live here.

like image 56
Paolo Avatar answered Sep 22 '22 10:09

Paolo


I think this regex will do what you want:

^(.*?)\.(?=(?:[0-9]+\.){2,}[0-9]+(?:-[a-z]+)?\.nupkg)(.*?)\.nupkg$

It uses a positive lookahead to look for the version number followed (possibly) by a tag in the form -[a-z]+ (e.g. -alpha) followed by \.nupkg. This last part prevents it matching the 4.0.0-armel in the third sample. For your edge cases, and substituting with Package: $1 Version: $2 the output is:

Package: knockoutjs Version: 3.4.2
Package: log4net Version: 2.0.8
Package: runtime.tizen.4.0.0-armel.microsoft.netcore.jit Version: 2.0.0
Package: nuget.core Version: 2.7.0-alpha
Package: microsoft.identitymodel Version: 6.1.7600.16394

Demo

like image 22
Nick Avatar answered Sep 20 '22 10:09

Nick