2014-10-27 88 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