[英]Check if a string contains a Version with more than 3 decimal points
我試圖得到一個程序的子版本,這將返回以下字符串Release 4.2.3 of programX
但有時也會Release 4.3 of programX
。 我想檢查字符串是否包含3個或更多小數位(如4.3.2)。
我發現這個問題有答案,但它不適用於C#,這在C#中是否可行,如果是這樣,最佳方法是什么?
使用linq。
bool more3dec = input.Count(c => c == '.') >= 2;
或正則表達式。 (僅檢查數字部分內的點)
bool more3dec = Regex.IsMatch(input, @"\d+\.\d+\.\d+");
如果你想知道它是否有2 periods .
你可以這樣做2 periods .
或者不是那么你可以這樣做。
var versionStr = "Release 4.2.3";
var nuberDecimals = versionStr.Split('.').Length - 1;
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.