简体   繁体   English

类型匹配的值不能转换为字符串

[英]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.

相关问题 VB.NET类型“字符串”的值不能转换为数据库 - VB.NET Value of type 'String' cannot be converted to DataBase 类型“字符串”的值不能转换为“一维字节数组” - Value of type 'string' cannot be converted to '1-dimensional array of byte' “T”类型的值无法转换为 - Value of type 'T' cannot be converted to 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 类型“ catalogAttributeEntity()”的值不能转换为“ catalogAttributeEntity” - Value of type “catalogAttributeEntity()” cannot be converted to “catalogAttributeEntity” “字符串”的值无法转换为“T”类型。 用于获取查询字符串的通用 function? - Value of 'String' cannot be converted to Type 'T'. Generic function for getting query strings? WPF:“'String' 类型的值无法转换为 'System.Windows.Media.ImageSource'。” - WPF: “Value of type 'String' cannot be converted to 'System.Windows.Media.ImageSource'.” BulkCopy错误”`来自数据源的String类型的给定值无法转换为指定目标列的int类型。 - BulkCopy Error" `The given value of type String from the data source cannot be converted to type int of the specified target column.` TypeLoadExceptionHolder无法转换为类型 - TypeLoadExceptionHolder cannot be converted to type 无法使用实体框架将参数“提示”转换为类型“字符串” - Argument 'Prompt' cannot be converted to type 'String' with Entity Framwork
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM