如何在c#中检查字符串是否为xxxxx.xxxx格式?

时间:2014-01-27 19:47:49

标签: c# string

我有一个字符串,应该是xxxxx.xxxx格式。我的应用程序自动获取此字符串,并且某些时间字符串看起来像xxxx.xxxx或xxxxxx.xxx或...

我的问题是“如何检查字符串是否格式正确?”

我想的是:

myString.Length == 10;

但如果dot位于错误位置可能会出错...

2 个答案:

答案 0 :(得分:4)

这是您可以使用的正则表达式:

\w{5}\x2E\w{4}

//      Alphanumeric, exactly 5 repetitions
//      Hex 2E (.)
//      Alphanumeric, exactly 4 repetitions

您应该从http://www.ultrapico.com

尝试Expresso

答案 1 :(得分:0)

严格来说,这将有效:

if (myString.Length == 10 && myString.IndexOf('.') == 5)
{
    //do something


}

if (myString.Length == 10 && myString.CharAt(5) == '.')
{
    //do something


}

如果'x'必须等于字母数字字符,那么这将起作用:

Match match = Regex.Match(input, @"[A-z0-9]{5}\.[A-z0-9]{4}$");
if (match.Success)
{


    //do something
}