简体   繁体   English

检查字符串是否包含超过3个小数点的版本

[英]Check if a string contains a Version with more than 3 decimal points

I am trying to get a the sub releases of a program, and this returns the following string Release 4.2.3 of programX but also sometimes: Release 4.3 of programX . 我试图得到一个程序的子版本,这将返回以下字符串Release 4.2.3 of programX但有时也会Release 4.3 of programX I want to do a check if the string contains 3 or more decimal places (like 4.3.2). 我想检查字符串是否包含3个或更多小数位(如4.3.2)。
I found This question with answer but it's not for C#, is this possible in C# and if so what would be the best approach? 我发现这个问题有答案,但它不适用于C#,这在C#中是否可行,如果是这样,最佳方法是什么?

Use linq. 使用linq。

bool more3dec = input.Count(c => c == '.') >= 2;

Or regex. 或正则表达式。 (Checking only points within numeric part) (仅检查数字部分内的点)

bool more3dec = Regex.IsMatch(input, @"\d+\.\d+\.\d+");

you can do this quite a view ways but if you want to know if it have 2 periods . 如果你想知道它是否有2 periods .你可以这样做2 periods . or not then you can do this for instance. 或者不是那么你可以这样做。

var versionStr = "Release 4.2.3";
var nuberDecimals = versionStr.Split('.').Length - 1;

nuberDecimals = 2; nuberDecimals = 2;

var input = "Release 4.2.3 andSomeText.With.Dots.Or.Namespaces";
var isValid = Regex.IsMatch(input.Split(' ')[1], @"\d+\.\d+\.\d+");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM