我正在搜索文件以查找一串单词。例如“一二三”。我一直在使用:使用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