我正在编写一个小应用程序,它将打开Word文档格式的状态报告。该文件有几个日期,但我想查找和更改的日期是这样的:
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,但我认为我不是那么聪明。
答案 0 :(得分: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