2013-02-17 109 views
6

我正在研究一个项目,我正在阅读的文件可能有两种不同的格式,一种包含日期和时间,另一种则不包含。查找字符串中是否包含日期和时间

当我在第一行读取时,我需要检查字符串是否包含日期和时间,并读取文件,并根据检查以某种方式读取文件。

我猜这将是一种正则表达式,但不知道从哪里开始,找不到任何相关的东西。

感谢您提供的任何帮助。

UPDATE 我不认为我一直都很清楚自己在问什么。当我逐行读取日志文件行的行可能会在如:

Col1 Col2 Col3 Col4 Col5 

有时行可能会在为

Col1 17-02-2013 02:05:00 Col2 Col3 Col4 Col5 

当我读了线,我需要做的检查是否有是字符串中包含的日期和时间字符串。

+0

你能分享你试过吗? – spajce 2013-02-17 01:49:02

+0

@Boardy:你确切知道在哪些日期时间区域设置格式中这些潜在日期将被表示为? dd-mm-yyyy hh:mm:ss总是? – 2013-02-17 02:11:38

+0

@ bob-the-destroyer它应该总是以dd-mm hh:mm:ss的格式。我不认为格式会发生变化(注意不包括年份)。我忘了提及早些时候 – Boardy 2013-02-17 02:20:44

回答

11

如果日期的格式已经被定义,你可以使用正则表达式来解决这个问题。

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(); 
     } 
    } 
} 
+0

我改进了正则表达式的例子 – amhed 2013-02-17 13:42:35

+0

非常感谢你的帮助非常感谢。 – Boardy 2013-02-17 23:38:18

5

更新2:使用DateTime.TryParseExact找到了一个更好的方法是使用正则表达式的表达式

DateTime myDate; 
if (DateTime.TryParseExact(inputString, "dd-MM-yyyy hh:mm:ss", 
    CultureInfo.InvariantCulture, DateTimeStyles.None, out myDate)) 
{ 
    //String has Date and Time 
} 
else 
{ 
    //String has only Date Portion  
} 
+0

我认为OP要检查日期和时间:) – scartag 2013-02-17 01:50:02

+0

@scartag:如果xdatetime == DateTime.MinValue,它表示尝试失败。幸运的是,如果'DateTime.TryParse(x)'失败,它将返回false,所以你不必担心这个最小值检查。 – 2013-02-17 01:56:45

+0

如果TryParse失败,则DateTime对象保持默认值(即DateTime.MinValue)。 – amhed 2013-02-17 01:58:06

-2

这个工作对我们:

如果字符串值是有效的datetime值,则它不会给任何异常:

try 
{ 
    Convert.ToDateTime(string_value).ToString("MM/dd/yyyy"); 
} 

如果字符串值日期时间值无效,则会发生异常:

catch (Exception) 
{ 
} 
+0

虽然代码是赞赏的,但它应该总是有一个附随的解释。这并不需要很长时间,但它是可以预料的。 – peterh 2015-09-30 17:11:51

+0

@peterh - 更新后。 – xameeramir 2015-10-01 06:20:41

+0

我不是很了解你,如果你以一种不需要我的后期修复的方式来做,你已经从我这里得到了赞赏。 – peterh 2015-10-02 11:50:34

-2

使用此方法来检查字符串日期与否:

private bool CheckDate(String date) 
{ 
    try 
    { 
     DateTime dt = DateTime.Parse(date); 
     return true; 
    } 
    catch 
    { 
     return false; 
    } 
} 
+2

虽然代码是赞赏,它应该总是有一个附带的解释。这并不需要很长时间,但它是可以预料的。 – peterh 2015-09-30 17:11:39

+0

我记下了你的建议。谢谢 – 2015-09-30 17:25:44

0
string s = "Subject: Current account balances for 21st December 2017 
    mnmnm ";//here is u r sample string 

      s = s.ToLower(); 
      string newStrstr = Regex.Replace(s, " {2,}", " ");//remove more than whitespace 
      string newst = Regex.Replace(newStrstr, @"([\s+][-/./_///://|/$/\s+]|[-/./_///://|/$/\s+][\s+])", "/");// remove unwanted whitespace eg 21 -dec- 2017 to 21-07-2017 
      newStrstr = newst.Trim(); 
      Regex rx = new Regex(@"(st|nd|th|rd)");//21st-01-2017 to 21-01-2017 
      string sp = rx.Replace(newStrstr, ""); 
      rx = new Regex(@"(([0-2][0-9]|[3][0-1]|[0-9])[-/./_///://|/$/\s+]([0][0-9]|[0-9]|[1][0-2]|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|january|february|march|april|may|june|july|august|september|october|november|december)[-/./_///:/|/$/\s+][0-9]{2,4})");//a pattern for regex to check date format 
      Match m = rx.Match(sp);//look for string satisfy the above pattern regex 
      s = m.ToString();//convert matching data to string 


     DateTime st = Convert.ToDateTime(s);//converting that data 
+0

这将从任何一个字符串中取出日期 – 2017-12-11 06:01:15

+0

查看上面的代码解释更多。 – 2017-12-12 06:17:59

相关问题