Определить, найдены ли конкретные слова .Net - PullRequest
0 голосов
/ 12 ноября 2018

Здравствуйте, я пытаюсь найти конкретные слова, представляющие число

Пример:
- число "Один" в RichTextBox

Мой код:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim str As String = RichTextBox1.Text
    Dim strarr() As String
    strarr = str.Split(" "c)
    For Each s As String In strarr

        Dim words() As String = s.ToLower.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
        If words.Count(Function(w) RichTextBox2.Text.Contains(w)) > 0 Then

            Label1.Text = s
            Label1.Text = "Founded"
        Else
            Label1.Text = "not founded, if we find it, we will type it , in label1"
        End If
    Next

End Sub

RichTextBox2 = список моих слов (1 - 5) цифр.
RichTextBox1 = на котором я сосредоточен.

Проблема заключается в том, что я набираю RichTextBox1.text
hello i want type numbers, on
он обнаружит «вкл» как (один).Это не моя цель.

объяснение К картинке

1 Ответ

0 голосов
/ 12 ноября 2018

Я думаю, что это может работать лучше:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' first remove any previous Label1 text
    Label1.Text = ""

    ' cleanup the RichTextBox2 text by replacing any whitespace or non-word character by a single space character
    ' make it all lowercase and trim off the spaces left and right
    Dim keyText As String = (Regex.Replace(RichTextBox2.Text, "[\s\W]+", " ")).ToLower().Trim()
    ' next, split it into an array of keywords
    Dim keyWords As String() = keyText.Split(" "c)

    ' get the user input and prepare it for splitting into words like we did with the RichTextBox2 text
    Dim input As String = (Regex.Replace(RichTextBox1.Text, "[\s\W]+", " ")).ToLower().Trim()

    ' split the cleaned-up input string into words and check if they can be found in the keyWords array
    ' if we do find them, we want only list them once so collect them first in a List
    Dim wordsFound As New List(Of String)
    For Each word As String In input.Split(" "c)
        If keyWords.Contains(word.ToLower()) Then
            If Not (wordsFound.Contains(word)) Then
                wordsFound.Add(word)
            End If
        End If
    Next
    ' finally, add the result to the label
    Label1.Text = String.Join(Environment.NewLine, wordsFound)
End Sub

demo

...