0

私のウィンドウアプリケーションでは、文から正確な単語を見つけたいと思っています.2つのフィールドは、それぞれテキストボックス(単語)とリッチテキストボックス(文)になります。

私は Str(i).contains(word) と For Each match As Match In Regex.Matches(str(i), word) を使用しました。

しかし、これら 2 つは正確な単語を取得しません。

上記では、create で ate を取得していますが、結合されていない ate のみに焦点を当てる必要があります。

4

2 に答える 2

4

Regex.Matches では、\b 単語境界文字を使用する必要があります。C# でサンプル コードを書いたところ、あなたの質問が VB.NET に関するものであることに気付きました。両方のコード例を追加します。

C#:

        //Example 1:
        var testString = "parfaite ment aux exigences create évolutives du marché d aujourd hui";
        var pattern = "ate";
        MatchCollection found = Regex.Matches(testString, @"\b" + pattern + @"\b");

        if (found.Count > 0)
        {
            foreach (Match f in found)
            {
                Console.WriteLine("'{0}' found at position {1} in given testString.", f.Value, f.Index);
            }
        }
        else Console.WriteLine("No matches in given testString.");


        //Example 2:
        var testString1 = "parfaite ment aux exigences cre*ate* évolutives du marché d aujourd hui I don't know the language but this: ate and the last one should be found: ate!";
        var pattern1 = "ate";
        MatchCollection found1 = Regex.Matches(testString1, @"\b" + pattern1 + @"\b");

        if (found1.Count > 0)
        {
            foreach (Match f in found1)
            {
                Console.WriteLine("'{0}' found at position {1} in given testString1.", f.Value, f.Index);
            }
        }
        else Console.WriteLine("No matches in given testString1.");

        Console.ReadLine();

VB.NET:

    'Example 1:
    Dim testString = "parfaite ment aux exigences create évolutives du marché d aujourd hui"
    Dim pattern = "ate"
    Dim found As MatchCollection = Regex.Matches(testString, "\b" & pattern & "\b")

    If found.Count > 0 Then
        For Each f In found
            Console.WriteLine("'{0}' found at position {1} in given testString.", f.Value, f.Index)
        Next
    Else
        Console.WriteLine("No matches in given testString.")
    End If

    'Example 2:
    Dim testString1 = "parfaite ment aux exigences cre*ate* évolutives du marché d aujourd hui I don't know the language but this: ate and the last one should be found: ate!"
    Dim pattern1 = "ate"
    Dim found1 As MatchCollection = Regex.Matches(testString1, "\b" & pattern1 & "\b")

    If (found1.Count > 0) Then

        For Each f As Match In found1
            Console.WriteLine("'{0}' found at position {1} in given testString1.", f.Value, f.Index)
        Next
    Else
        Console.WriteLine("No matches in given testString1.")
    End If

    Console.ReadLine()
于 2013-05-14T07:02:06.123 に答える
0

多分これは助けになるでしょうか?

Private Sub SearchForWord()
    Dim SearchString() As String = Split("ate parfaite ment aux exigences cre*ate* évolutives du marché d aujourd hui", " ")
    Dim WordToFind As String = "ate"
    Dim count As Integer = 0
    Dim NumMatches As Integer = 0

    Do Until count = UBound(SearchString)
        If UCase$(WordToFind) = UCase$(SearchString(count)) Then
            NumMatches = NumMatches + 1
        End If
        count = count + 1
    Loop

    Debug.Print(Format(NumMatches))

End Sub
于 2013-05-14T19:43:24.100 に答える