如何确定字符串是否包含任何格式的特定日期?

时间:2014-03-24 16:00:39

标签: c# .net vb.net

我有一个对象,其属性A为DATE类型,属性B为string类型。要求是属性B可能不包含属性A的字符串表示。

现在我有一种非常简单的验证方法:

public function Validate() as boolean
    if b.contains(a.toString("[format1]", CultureInfo....)
      return false
    end if

    if b.contains(a.toString("[format2]", CultureInfo...)
       return false
    end if

    'etc

    return true

end function

但这种方法的某些方法感觉不对。 Date.TryParse不起作用,因为B可能比JUST A有更多。

是否有一些方法可以让我验证B而不输入A的每种可能的日期时间格式(在各种文化中)?

我不关心解决方案是VB.net还是C#。

澄清: 属性B有一些格式限制。它不允许正斜杠或点或偶数空格的典型日期分隔符。所以我希望看到像mmddyyyy和ddmmyyyy甚至是MONddyyyy等的日期。除了月,日和年之外,我不担心任何事情。

我可以保留一份可能的格式列表并进行迭代,但我担心的是我可能会忽略这种格式。

额外澄清 属性A是日期值,而不是字符串。因此格式不是由用户决定的 - 它由我的验证过程决定。因此,在以下示例中,B不应该验证。

A = Date(1962, 01, 22)

B = 01221962MyNewString
or
B = Mystring19620122value
or
B = Jan221962mystring
etc.

我需要排除字符串表示的许多可能性。虽然我想,我不需要排除“the22ofjanuary1962”。

我可以使用正则表达式 - 但存在同样的问题。我只需要考虑每个可能的字符串表示并检查它。我希望.Net框架中的某些东西已经存在,我可以使用它。但听起来没有这样的运气。

解答: 我把Blam的帖子标记为答案。它让我非常接近,特别是一旦我添加了正则表达式。我遍历所有可能的文化。然后,我将浏览所有相关的标准日期时间格式(http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx),删除所有非字母字符,并进行比较。

不是说它抓住了所有东西,但我所有的单元测试都通过了,所以它抓住了我今天想到的所有东西。如果需要变得明显,我总是可以在以后添加自定义日期时间格式。

这是我的解决方案。

Dim allCultures As CultureInfo()

allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
Dim rgx = New Regex("[^a-zA-Z0-9]")

For Each ci As CultureInfo In allCultures
 If B.Contains(rgx.Replace(Me.A.ToString("d", ci), "")) Or _
                B.Contains(rgx.Replace(Me.A.ToString("D", ci), "")) Or _
                B.Contains(rgx.Replace(Me.A.ToString("M", ci), "")) Or _
                B.Contains(rgx.Replace(Me.A.ToString("s", ci), "")) Or _
                B.Contains(rgx.Replace(Me.A.ToString("u", ci), "")) Or _
                B.Contains(rgx.Replace(Me.A.ToString("Y", ci), "")) Or _
                B.Contains(rgx.Replace(Me.A.ToString("g", ci), "")) Then
                Return False
     End If
Next

Return True

2 个答案:

答案 0 :(得分:1)

可以枚举文化

CultureTypes[] mostCultureTypes = new CultureTypes[] {
            CultureTypes.NeutralCultures, 
            CultureTypes.SpecificCultures, 
            CultureTypes.InstalledWin32Cultures, 
            CultureTypes.UserCustomCulture, 
            CultureTypes.ReplacementCultures, 
            };
CultureInfo[] allCultures;
DateTime dt = new DateTime(1962, 01, 22);

// Get and enumerate all cultures.
allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo ci in allCultures)
{
    // Display the name of each culture.
    Console.WriteLine("Culture: {0}", ci.Name);
    Thread.CurrentThread.CurrentCulture = ci;
    Console.WriteLine("Displaying short date for {0} culture:",
                        Thread.CurrentThread.CurrentCulture.Name);
    Console.WriteLine("   {0} (Short Date String)",
                        dt.ToShortDateString());
    Console.WriteLine();
}

即使你看来你不允许空格,所以你需要移除空格 您不允许使用某些分隔符,因此需要将其删除 您说明了任何数据格式,但您确实在寻找特定的日期格式 但您不想识别日期格式,因为您可能会遗漏日期格式。

答案 1 :(得分:0)

我想一个选项是在某处保留可能的格式列表,例如:一些常量或在配置文件中,然后您可以迭代该列表并使用每种格式的日期创建一个新列表。完成后,您可以检查字符串是否包含任何值。

相关问题