[英]Value of Type Match Cannot Be Converted To String
我目前正試圖遍歷文本文件的文件夾並讀取它們。 閱讀它們之后,我想使用正則表達式提取文件名稱的特定部分,但是出現Value of Type Match Cannot Be Converted To String
的錯誤Value of Type Match Cannot Be Converted To String
。
我曾嘗試使用Cstr
但這似乎無法解決我的問題。
我正在使用的代碼:
Dim fileentries As String() = Directory.GetFiles("D:\User\BackUp\Project\bin\Debug\Orders")
For Each entry In fileentries
Dim match As New List(Of String)
Dim regexmatch As Match = Regex.Match(entry, "Order_\d\d-\d\d-[\d]{4}_[\d]{6}")
match.Add(CStr(regexmatch))
Next
這里正則表達式部分正在工作,似乎提取了我想要的文件名的正確部分,但是特別是使用line match.Add(Cstr(regexmatch))
我遇到了我描述的錯誤。
任何幫助表示贊賞,謝謝。
您需要訪問Match
對象的.Value
屬性,但是建議檢查是否完全匹配:
Dim regexmatch As Match = Regex.Match(entry, "Order_\d\d-\d\d-\d{4}_\d{6}")
If regexmatch.Success Then
match.Add(regexmatch.Value)
End If
參見VB.NET演示 :
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
' ...
Dim match As New List(Of String)()
Dim entry As String = "XXXX_Order_12-12-1234_123456_irrelevant.txt"
Dim regexmatch As Match = Regex.Match(entry, "Order_\d\d-\d\d-\d{4}_\d{6}")
If regexmatch.Success Then
match.Add(regexmatch.Value)
End If
Console.WriteLine(match(0)) ' => Order_12-12-1234_123456
注意[\\d]{4}
等於\\d{4}
,不需要將單個原子放入字符類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.