2014-10-27 90 views
0

我正在搜索文件以查找一串單詞。例如「一二三」。我一直在使用:使用VB.NET掃描文件中的一串字忽略多餘的空格

Dim text As String = File.ReadAllText(filepath) 
For each phrase in phrases 
    index = text.IndexOf(phrase, StringComparison.OrdinalIgnoreCase) 
    If index >= 0 Then 
     Exit For 
    End If 
Next 

和它的工作很好,但我現在已經發現有些文件可能包含目標短語與字之間不止一個空格差距。

例如我的代碼發現

one two three」,但未能找到「one two three

是有辦法,我可以使用正則表達式,或任何其他技術,捕捉到這句話即使之間的距離單詞不止一個空格?

我知道我可以使用

Dim text As String = File.ReadAllText(filepath) 
For each phrase in phrases 
    text=text.Replace(" "," ") 
    index = text.IndexOf(phrase, StringComparison.OrdinalIgnoreCase) 
    If index >= 0 Then 
     Exit For 
    End If 
Next 

,但我想知道是否有實現這一

+0

您可以硬編碼'phrases'爲包含'\ S *'詞語,其中'phrases'現在regex對象的陣列之間。或者,您可以將每個短語構造爲正則表達式,在其中用'\ s +'替換'[] +',然後將其用作正則表達式。 – sln 2014-10-27 18:04:20

回答

1

您可以創建一個刪除任何雙重空格的函數。

Option Strict On 
Option Explicit On 
Option Infer Off 
Public Class Form1 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim testString As String = "one two three four five  six" 
     Dim excessSpacesGone As String = RemoveExcessSpaces(testString) 
     'one two three four five six 
     Clipboard.SetText(excessSpacesGone) 
     MsgBox(excessSpacesGone) 
    End Sub 
    Function RemoveExcessSpaces(source As String) As String 
     Dim result As String = source 
     Do 
      result = result.Replace(" ", " "c) 
     Loop Until result.IndexOf(" ") = -1 
     Return result 
    End Function 
End Class 
+0

事情並不總是雙重空間。我永遠無法預測空間的數量 – ElenaDBA 2014-10-30 16:10:33

+0

三倍空間,四倍等......我剛剛將它命名爲錯誤。它應該被命名爲removeExcessSpaces。測試它會刪除大於一個的空格。 – 2014-10-30 17:46:29

+0

非常好!謝謝! – ElenaDBA 2014-11-07 14:12:05

0

你可以在你的短語轉換成正則表達式與\s+每個字之間更有效的方式,和然後檢查相應的文本。例如

Dim text = "This contains one Two three" 
Dim phrases = { 
    "one two three" 
} 
' Splits each phrase into words and create the regex from the words. 
For each phrase in phrases.Select(Function(p) String.Join("\s+", p.Split({" "c}, StringSplitOptions.RemoveEmptyEntries))) 
    If Regex.IsMatch(text, phrase, RegexOptions.IgnoreCase) Then 
     Console.WriteLine("Found!") 
     Exit For 
    End If 
Next 

請注意,這並不在這句話的開始/結束檢查單詞邊界,所以"This contains someone two threesome"也將匹配。如果你不想要,在正則表達式的兩端添加"\s"

+0

謝謝,我會試試 – ElenaDBA 2014-10-30 16:10:48

+0

我收到一個錯誤:Lambda表達式不能轉換爲'整數',因爲'整數'不是委託類型。並且這部分被突出顯示「Function(p)String.Join(」\ s +「,p.Split({」「c},StringSplitOptions.RemoveEmptyEntries))」 – ElenaDBA 2014-10-30 16:15:21

+0

@ElenaDBA我剛纔複製了上面的代碼並將其直接粘貼到[ LINQPad](http://www.linqpad.net/),它的工作沒有錯誤,所以不知道問題可能是什麼。在一個新的控制檯項目(VS2013,.NET 4.5.2)的'Main'方法中,我只需要爲'System.Text.RegularExpressions'添加一個導入。也許在某個地方有一個錯字? – Mark 2014-10-30 16:26:18

1

評論中的代碼將解釋代碼

 Dim inputStr As String = "This contains one  Two three and some  other words" '<--- this be the input from the file 
     inputStr = Regex.Replace(inputStr, "\s{2,}", " ") '<--- Replace extra white spaces if any 
     Dim searchStr As String = "one two three" '<--- be the string to be searched 
     searchStr = Regex.Replace(searchStr, "\s{2,}", " ") '<--- Replace extra white spaces if any 
     If UCase(inputStr).Contains(UCase(searchStr)) Then '<--- check if input contains search string 
      MsgBox("contains") '<-- display message if it contains 
     End If