简体   繁体   English

将正则表达式匹配项添加到数组

[英]Adding Regex matches to an array

I'm struggling to solve a small bit of code. 我正在努力解决一小段代码。 What the code does is to first load a CSV file, line by line (starting by line 3), and add it to an array. 该代码的作用是首先逐行加载CSV文件(从第3行开始),然后将其添加到数组中。 Then run a regex match and I want to insert the value in an array. 然后运行正则表达式匹配,我想将值插入数组。

This is my working code, it shows a msgbox with the actual matches: 这是我的工作代码,它显示了具有实际匹配项的msgbox:

Dim file = "C:\Users\David\Desktop\mycsv.csv"
    Dim basestatisticspath = Path.GetDirectoryName(file)
    Dim statistics() As String = IO.File.ReadAllLines(file)
    'Dim x As Integer = statistics.Length
    'ReDim Preserve statistics(x)

    Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
    Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
    Dim linefilename As Match = Nothing
    Dim langmatch As Match = Nothing
    Dim filename() As String
    Dim lang() As String

    For i = 2 To UBound(statistics)
        langmatch = regexlang.Match(statistics(i))
        linefilename = regexlinefilename.Match(statistics(i))
        MsgBox(langmatch.Value & linefilename.Value)
    Next

That works well and the actual matches is what I want. 效果很好,实际匹配是我想要的。 So my next step was to add each match to an array to then use it to generate other files. 所以我的下一步是将每个匹配项添加到一个数组中,然后使用它来生成其他文件。

Therefore I ended up with this: 因此,我最终得到了这一点:

    Dim file = "C:\Users\David\Desktop\myscv.csv"
    Dim basestatisticspath = Path.GetDirectoryName(file)
    Dim statistics() As String = IO.File.ReadAllLines(file)
    'Dim x As Integer = statistics.Length
    'ReDim Preserve statistics(x)

    Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
    Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
    Dim linefilename As Match = Nothing
    Dim langmatch As Match = Nothing
    Dim filename() As String
    Dim lang() As String

    ' Set all value line by line
    For i = 2 To UBound(statistics)
        langmatch = regexlang.Match(statistics(i))
        linefilename = regexlinefilename.Match(statistics(i))
        lang(i) = langmatch.Value.ToString
        filename(i) = linefilename.Value.ToString
        MsgBox(langmatch.Value & linefilename.Value)

    Next

After adding the below the program crashes on that line 添加以下内容后,程序在该行崩溃

    lang(i) = langmatch.Value.ToString
    filename(i) = linefilename.Value.ToString

I am assuming you can add the value of a regex match to a certain position of a string, but it seems I am wrong. 我假设您可以将正则表达式匹配的值添加到字符串的某个位置,但是似乎我错了。

I´ve been searching for an answer with no results (at least to my poor understanding). 我一直在寻找没有结果的答案(至少是对我的理解不好)。

How could I convert each of the matches to a string and add it to the i position of the array? 如何将每个匹配项转换为字符串并将其添加到数组的i位置?

Thanks in advance! 提前致谢!

UPDATE: As @Tval explained, I solved it by including the size of the array when declaring it. 更新:正如@Tval解释的那样,我在声明它时通过包括数组的大小来解决它。 Thanks! 谢谢!

    Dim filename(UBound(statistics)) As String
    Dim lang(UBound(statistics)) As String

you need to initialize the array before you can reference it or you'll get a null reference error, also you can't reference an index that doesn't exist yet or you'll get an index out of range exception. 您需要先初始化数组,然后才能引用它,否则将得到null引用错误,也无法引用尚不存在的索引,否则将获得索引超出范围的异常。

right now your using an array with a fixed length, so if you want to add a value to it you'll have to re-declare it one index larger every time. 现在,您使用固定长度的数组,因此,如果要向其中添加值,则每次都必须重新声明一个更大的索引。

If you want an array of a variable length id suggest using a list, so you can just append values to it without any issues 如果您想要一个可变长度id的数组,建议使用列表,这样就可以在其上附加值而不会出现任何问题

Dim myList = New List(Of String)
For Each foo As String In bar
    myList.Add(bar)
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM