[英]Value of Type Match Cannot Be Converted To String
I'm currently trying to loop over a folder of text files and read them. 我目前正试图遍历文本文件的文件夹并读取它们。 After I've read them, I'd like to extract a certain portion of the name of the files using Regular Expressions, however I am getting the error 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
。
I have tried using Cstr
however this doesn't seem to solve my problem. 我曾尝试使用Cstr
但这似乎无法解决我的问题。
Code I am using: 我正在使用的代码:
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
Here the Regular Expressions section is working, it seems to extract the correct portion of the filename I want however specifically with line match.Add(Cstr(regexmatch))
I am getting the error I've described. 这里正则表达式部分正在工作,似乎提取了我想要的文件名的正确部分,但是特别是使用line match.Add(Cstr(regexmatch))
我遇到了我描述的错误。
Any help is appreciated, thank you. 任何帮助表示赞赏,谢谢。
You need to access the .Value
property of the Match
object, but it is recommended to check if there is a match at all: 您需要访问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
See the VB.NET demo : 参见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
Note that [\\d]{4}
is equal to \\d{4}
, no need to put a single atom into a character class. 注意[\\d]{4}
等于\\d{4}
,不需要将单个原子放入字符类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.