Vb.net 多索引搜索

Vb.net 多索引搜索,vb.net,Vb.net,我已经编写了一个代码来搜索文本,使用索引不保存到列表框。 我需要添加更多的字符来搜索。 我需要找到以-,结尾->开头的文本,该文本不会写入列表框。 他们还需要查找以/**开头、以*/结尾的文本,但此代码不再有效 Dim openfile = New OpenFileDialog() With {.Filter = "Text (*.php)|*.php"} If (openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then

我已经编写了一个代码来搜索文本,使用索引不保存到列表框。 我需要添加更多的字符来搜索。 我需要找到以-,结尾->开头的文本,该文本不会写入列表框。 他们还需要查找以/**开头、以*/结尾的文本,但此代码不再有效

Dim openfile = New OpenFileDialog() With {.Filter = "Text (*.php)|*.php"}
If (openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
    For Each line As String In File.ReadAllLines(openfile.FileName, Encoding.Default)
        'OPEN DIALOG
    Next

End If

Dim komentar As Boolean

komentar = False
For Each line As String In File.ReadAllLines(openfile.FileName, Encoding.Default)
    'FIND Charakter <!- and stop write to listbox
    If Not line.IndexOf("<!-") = -1 Then
        komentar = True
    End If

    If komentar Then --- this wrong
    'FIND Charakter --> and afrer write to listbox  
    If Not line.IndexOf("-->") = -1 Then
        komentar = False
    End If
    ' I don't know how to specify another condition to search for other sentences 
    ' that don't write when it finds characters
    If Not line.IndexOf("/**") = -1 Then
        komentar = True
    End If

    If komentar Then
        If Not line.IndexOf("*/") = -1 Then
            komentar = False
        End If
    Else
        ListBox1.Items.Add(line)
    End If
Dim openfile=New OpenFileDialog()和{.Filter=“Text(*.php)|*.php”}
如果(openfile.ShowDialog()=System.Windows.Forms.DialogResult.OK),则
对于文件.ReadAllLines(openfile.FileName,Encoding.Default)中的字符串形式的每一行
'打开对话框
下一个
如果结束
Dim komentar为布尔型
科门塔=假
对于文件.ReadAllLines(openfile.FileName,Encoding.Default)中的字符串形式的每一行

“查找Charakter一开始我误解了你的要求,但安德鲁·莫顿的评论让我意识到你真正的要求是什么。以下是我可以如何实现它:

Dim terminatorsByInitiator As New Dictionary(Of String, String) From {{"<!--", "-->"},
                                                                      {"/**", "*/"}}
Dim isComment = False
Dim terminator As String

For Each line In File.ReadAllLines(openfile.FileName, Encoding.Default)
    If isComment Then
        'Check whether we are at the end of the comment.
        If line.EndsWith(terminator) Then
            isComment = False
        End If
    Else
        'Get the comment initiator at the start of the line, if there is one.
        Dim initiator = terminatorsByInitiator.Keys.FirstOrDefault(Function(s) line.StartsWith(s))

        If initiator Is Nothing Then
            'This line does not initiate a comment so add it to the list.
            ListBox1.Items.Add(line)
        Else
            'This line does initiate a comment.
            isComment = True

            'Remember which comment terminator to look for.
            terminator = terminatorsByInitiator(initiator)
        End If
    End If
Next
Dim terminatorsByInitiator作为来自{{{'}的新字典(字符串的,字符串的),
{"/**", "*/"}}
Dim isComment=False
作为字符串的暗淡终止符
对于File.ReadAllLines(openfile.FileName,Encoding.Default)中的每一行
如果我同意的话
检查我们是否在评论的末尾。
如果行.EndsWith(终止符),则
isComment=False
如果结束
其他的
'在行的开头获取注释发起人(如果有)。
Dim启动器=terminatorsByInitiator.Keys.FirstOrDefault(函数行.启动方式))
如果启动器是空的,那么
'此行不会启动注释,因此请将其添加到列表中。
ListBox1.Items.Add(行)
其他的
'这一行确实启动了注释。
isComment=True
'记住要查找的注释终结符。
终止符=终止符ByInitiator(启动器)
如果结束
如果结束
下一个

使用
字典
可确保始终根据找到的启动器查找正确的注释终止符。

“此代码不再有效”。这永远不是一个可以接受的描述。您必须对问题提供完整、清晰的解释,其中包括代码的确切行为,以及这与您的期望有何不同。还有,那疯狂的缩进是怎么回事?当IDE可以为您格式化代码时,没有理由让代码难以阅读。如果你不能做出一点努力来帮助我们,那么我们就不太可能做出努力来帮助你。我正在编辑“询问”,我不知道如何更好地编写它
而不是line.IndexOf("我怀疑
/*
*/
是用于多行注释的。是的,注释/**和*/也可以在一行或多行上。我使用的是just Wasted indexof。Andrew Morton写的内容将使我只在列表框中添加注释,但其他内容都没有编写,我需要它。Option抱怨说
是对象。
终止符
没有被赋值?@Mary,如果您有
选项推断关闭
文件,您只会遇到
选项严格限制
的问题。ReadAllLines
返回一个
字符串
数组,因此
将被推断为类型
字符串