2013-05-14 51 views
0

在我的窗口應用程序中,我想從句子中找到確切的單詞。兩個字段分別是文本框(word)和richtextbox(句子)。我已經使用Str(i).contains(單詞)和For Each Match as Match In Regex.Matches(str(i),word)。如何從句子中找到確切詞

但是這兩個沒有得到確切的word.For例如, 字:吃 一句:parfaite換貨AUX exigences CRE * 在吃 *évolutivesduMarché廣場d aujourd輝

它上面的取在create.ed吃,但我需要集中onle不吃與結合。

+1

在你的**單詞前後添加空格**,不是? – 2013-05-14 05:54:21

+0

如何'使用'indexOf'。當它找到第一個匹配項時,請使用start index =第一個出現索引加上搜索字符串長度再次開始搜索。直到'indexOf'返回-1 – 2013-05-14 05:59:12

+0

謝謝阿德里安..在某些情況下,添加空間將無法提取,如單詞是聲明的結尾。可以爲第二個添加樣本嗎? – Jai 2013-05-14 06:01:43

回答

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() 
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