检测是否在.Net中找到特定单词

检测是否在.Net中找到特定单词,.net,regex,vb.net,visual-studio-2012,.net,Regex,Vb.net,Visual Studio 2012,你好,我试着找出表示数字的特定单词 例如: -RichTextBox中的数字“1” 我的代码: 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

你好,我试着找出表示数字的特定单词

例如:
-RichTextBox中的数字“1”

我的代码:

    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
你好,我想在

它会将“开”检测为(一)。这不是我的目的


我认为这可能更有效:

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

我想,在某种程度上,你可以得到你想要的,但只是为了确保:你想在一个框中写,点击一个按钮,然后在第一个框中找到的所有数字都被重新写在第二个框中?不要使用
包含()
,使用
等于()
,可能会添加
字符串比较。CurrentCultureInoRecase
<如果您还想知道找到了多少符合条件的单词及其在文本中的位置,那么code>Regex.Matches可能会更好。它永远不会显示找到的内容(字符串s中的内容),因为它会被创建的文本覆盖。你会看到,它将输出一些不同于你想象的东西。你太棒了,这就是我想要的,非常感谢,来自世界上最好的线程“StackoverFlow”的GREAT解决方案,帮助了很多人。