2

I'm writing a little application that will open my status reports that are in Word doc format. The document has several dates but the ones I want to find and change are like this:

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

I'm writing this in VB.NET I can use the String.IndexOf("Start Date: ") method and it finds the word, but I was hoping to get the index position, then using String.Length, get the date field to modify it for each of the start and end date dates.

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

I thought about using RegEx but I don't think I'm that clever.

4

1 回答 1

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
于 2012-10-03T14:44:11.353 回答