Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching version number parts with regular expressions

Tags:

c#

regex

version

I'm trying to match the parts of a version number (Major.Minor.Build.Revision) with C# regular expressions. However, I'm pretty new to writing Regex and even using Expresso is proving to be a little difficult. Right now, I have this:

(?<Major>\d*)\.(?<Minor>\d*)\.(?<Build>\d*)\.(?<Revision>\d*)

This works, but requires that every part of the version number exists. What I would like to do is also match versions like:

2.13

In this case, the Build and Revision groups need to return null values. Feel free to suggest a better method if I'm going about this all wrong.

like image 919
David Brown Avatar asked Dec 30 '08 15:12

David Brown


2 Answers

(?<Major>\d*)\.(?<Minor>\d*)(\.(?<Build>\d*)(\.(?<Revision>\d*))?)?

Makes the third and fourth parts optional.

like image 167
Jonathan Leffler Avatar answered Oct 01 '22 15:10

Jonathan Leffler


I know this isn't a regex, but System.Version does all the work for you.

like image 30
Blue Toque Avatar answered Oct 01 '22 15:10

Blue Toque