将版本号部分与正则表达式匹配 [英] Matching version number parts with regular expressions
问题描述
我正在尝试将版本号(Major.Minor.Build.Revision)的部分与C#正则表达式匹配.但是,我对编写Regex非常陌生,即使使用Expresso也被证明有点困难.现在,我有这个:
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.
推荐答案
(?<Major>\d*)\.(?<Minor>\d*)(\.(?<Build>\d*)(\.(?<Revision>\d*))?)?
将第三部分和第四部分设为可选.
Makes the third and fourth parts optional.
这篇关于将版本号部分与正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!