1 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
因此,您要檢查輸入是否采用以下兩種形式之一:12,345或12:34。
這可以Regex很容易地使用。
static void Main(string[] args)
{
var inputComma = "12,345";
var inputColon = "98:76";
Regex regexComma = new Regex(@"^\d{2},\d{3}$");
Regex regexColon = new Regex(@"^\d{2}:\d{2}$");
var matchComma = regexComma.Match(inputComma);
if (matchComma.Success)
{
Console.WriteLine(inputComma);
}
Console.WriteLine();
var matchColon = regexColon.Match(inputColon);
if (matchColon.Success)
{
Console.WriteLine(inputColon);
}
Console.ReadLine();
}
筆記:
您還沒(méi)有完全闡明輸入的有效格式。12,345如果存在逗號(hào)(即,兩位數(shù)字后跟一個(gè)逗號(hào),后跟三位數(shù)字),則以上將嚴(yán)格評(píng)估格式為真,而對(duì)于冒號(hào),只有格式的12:34數(shù)字(冒號(hào)前后兩位數(shù)字)。
您可能希望Regex根據(jù)您的確切標(biāo)準(zhǔn)修改您的。
- 1 回答
- 0 關(guān)注
- 199 瀏覽
添加回答
舉報(bào)