i have a string with 2 date-patterns "yyyyMMdd" and "yyyyMMddHHmmss" within a pair of <>:
"C:\Main\Program\Log\<yyyyMMdd>\infoReport_<yyyyMMddHHmmss>.log"
what i would like to do is replace the first date-pattern with "#", and the second with "@".
so that the result should be :
"C:\Main\Program\Log\#\infoReport_@.log"
Solution:
Imports System.Text.RegularExpressions
Module VBModule
private filepath As String ="C:\Main\Program\Log\<yyyyMMdd>\infoReport_<yyyyMMddHHmmss>.log"
private Const PREFIX As String = "<"
private Const SURFIX As String = ">"
Sub Main()
Console.WriteLine("result: " & replaceDatePatterns(filepath))
End Sub
function replaceDatePatterns(ByVal filepath as String) As String
Dim result As String = filepath
Dim regex As New Regex(String.Format("({0}).+?({1})", PREFIX, SURFIX))
Dim i As integer = 0
If NOt filepath.Contains(PREFIX) AndAlso not filepath.contains(SURFIX) then return result
For Each match As Match In regex.Matches(filepath)
Select case i
CASE 0 :
result = Replace(result, match.Value, "#")
CASE 1 :
result = Replace(result, match.Value, "@")
End Select
i +=1
Next
return result
End function
End Module
output:
result: "C:\Main\Program\Log\#\infoReport_@.log"
Reference:
https://vimsky.com/zh-tw/examples/detail/vbnet-method-system.text.regularexpressions.regex.matches.html
https://www.dotnetperls.com/regex-vbnet
https://www.dreamincode.net/forums/topic/118009-how-i-read-text-between-specific-begin-end-tags/
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement
No comments :
Post a Comment