2013-02-17 105 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

相關問題