Finding if a string contains a date and time

前端 未结 5 1162
渐次进展
渐次进展 2020-12-16 00:57

I am working on a project where I am reading in a file which could come in two different formats, one includes a date and time and the other doesn\'t.

When I read i

5条回答
  •  [愿得一人]
    2020-12-16 01:43

    If the format of the date has been defined, you can use Regex to solve it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace RegTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string testDate = "3214312402-17-2013143214214";
                Regex rgx = new Regex(@"\d{2}-\d{2}-\d{4}");
                Match mat = rgx.Match(testDate);
                Console.WriteLine(mat.ToString());
                Console.ReadLine();
            }
        }
    }
    

提交回复
热议问题