如何排除选定的文本并提取以“:”分隔的下一个文本?

时间:2015-04-13 08:58:29

标签: vb.net

我正在为我的母语制作机器可读字典,这是马来语。我需要从.txt文件中提取马来语翻译。在.txt文件中,示例如下:

畸变:aberasi

像差函数:rangkap aberasi

消融:ablasi

烧蚀材料:bahan ablasi

左边是英文术语,分隔符后面是:马来语。 我想问的是,当搜索一词是“畸变函数”而我只需要显示“rangkap aberasi”时我该怎么办?

我试过用

Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim text As String = "dictionary.txt"
        Dim word As String = "\b" & TextBox2.Text & "\b\s+(\w+)"

        For Each a As Match In Regex.Matches(text, word, RegexOptions.IgnoreCase)

            MsgBox(a.Groups(1).Value)
        Next
    End Sub
End Class

详情请见:http://www.visual-basic-tutorials.com/get-the-next-word-after-a-specific-word-in-visual-basic.htm#sthash.w8E1Qb6l.dpuf

然而,我的问题是,上面的代码只显示分隔后的下一个单词“rangkap”。虽然我需要分离后的全文“:”,这可能超过2个单词。

这是我当前的代码

Using reader As New StreamReader("D:\Dictionary of Engineering A only.txt")
    While Not reader.EndOfStream
        Dim line As String = reader.ReadLine()
        If line.Contains(wordsearch.Text) Then
            Edef.Text = line
            Exit While
        End If
    End While
End Using

1 个答案:

答案 0 :(得分:2)

解决这个问题的一种方法是:

Using reader As New StreamReader("D:\Dictionary of Engineering A only.txt")
    While Not reader.EndOfStream
        Dim line As String = reader.ReadLine()
        If line.Contains(wordsearch.Text) Then
            dim lineParts() as String = line.split(":")
            Edef.Text = lineParts(1)
            Exit While
        End If
    End While
End Using

检查该行是否包含文本,如果是,则将行拆分为“:”并取出第二部分。