2012-10-03 23 views
2

我在寫一個小應用程序,它將打開我的Word格式的狀態報告。該文件有幾個日期,但那些我想找到與變化是這樣的:如何查找和更改從.doc讀取的字符串中的日期

  • Start Date: 05/14/2012
  • End Date: 05/18/2012

我在VB.NET寫這我可以使用String.IndexOf("Start Date: ")方法並找到該詞,但我希望獲得索引位置,然後使用String.Length,獲取日期字段以針對每個開始日期和結束日期對其進行修改。

If (result.IndexOf("Start Date:") <> -1) Then 
    Console.WriteLine("Start Date Found!") 
End If 

我想過使用RegEx,但我不認爲我很聰明。

回答

1

這是一個正則表達式,將提取的日期爲您提供:

/(?:Start|End) Date: (\d{2}\/\d{2}\/\d{4})/

每個日期將在其自己的捕捉組,一旦你運行你的字符串這個表達式。

在VB中的一個例子:

Dim myMatches As MatchCollection 
Dim myRegex As New Regex("(?:Start|End) Date: (\d{2}\/\d{2}\/\d{4})") 
Dim t As String = "Start Date: 05/14/2012 End Date: 05/18/2012" 
myMatches = myRegex.Matches(t) 
' Search for all the words in a string 
Dim successfulMatch As Match 
For Each successfulMatch In myMatches 
    Debug.WriteLine(successfulMatch.Value) 
Next 
+0

這聽起來像他的新的正則表達式。爲什麼不提供一個例子? –

+1

@StevenDoggart我想不是一個壞主意。 –

+0

謝謝你的例子,它真的幫了很多。我對RegExp非常陌生我嘗試了幾個在線幫助工具,但由於報告中有很多其他日期,所以無法獲取模式。我應該能夠創建一個替換函數來改變這個日期。這個社區很棒! – ZGodwin

相關問題