嗨我有一個C#應用程序,它接受一個4位數的分機號碼,併爲它設置一個掩碼。我有兩種不同的口罩需要使用,具體取決於數量。正則表達式模式來區分四位數字
First: If the number starts with 47 or 5 return mask A.
Second: If the number starts with 6 or 55 return mask B.
所以我設置我的正則表達式這種方式,我不知道爲什麼它設置錯誤。
//Here I am trying to say, anything that start with 47 or 5 with the next 3 digits taking any number
Match first = Regex.Match(num, "^(47|(5[]{3}))");
//anything that start with 6 or 55 with the next 2 digits taking numbers 0-5
Match secong = Regex.Match(num, "(6|55[123450]{2})");
如果我給使用上面輸入NUM = 5850或num = 5511這將是雙方真實的,但顯然5850應該使用面膜A和5511應該使用掩模B
我該如何解決這個問題??
謝謝!
開始測試6或55第一,如果失敗,將檢查47或5 – Jerry
你絕對要做到這一點的一個正則表達式而不是添加一堆'if(input.StartsWith(「47」)){...}'子句? – millimoose
你可以移動你的兩個比賽的位置,使它匹配55之前,它會嘗試匹配5? – Andrew