我正在搜索文件以查找一串單詞。例如「一二三」。我一直在使用:使用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
,但我想知道是否有實現這一
您可以硬編碼'phrases'爲包含'\ S *'詞語,其中'phrases'現在regex對象的陣列之間。或者,您可以將每個短語構造爲正則表達式,在其中用'\ s +'替換'[] +',然後將其用作正則表達式。 – sln 2014-10-27 18:04:20