2011-09-11 59 views
0

我想在文本文件中同時搜索多個文字。根據文本文件的文字搜索文件

例如以爲我想搜索這3個字:馬吉德,超級用戶,設備

通常情況下,我應該通過一個搜索它們之一,我不能搜索所有的人在同一時間。所以我想在文本文件中同時搜索這些單詞。

我想在文本文件中輸入這3個單詞,每行一個單詞。我們將其命名爲SearchText。現在我有一個目標文本,我想在其中搜索這些單詞。我們將其命名爲TargetText。

我想告訴一個應用或類似的東西,從SearchText獲取單詞,並在TargetText中找到它們,並突出顯示它們或給我查找結果。

我希望我很清楚。那麼任何人都可以幫我嗎?

+0

呃......我們在說什麼語言?請將其添加爲標籤。 – Bojangles

回答

1

你很清楚。我認爲最好的選擇是正則表達式。
試試這個:

Option Explicit 
Dim oFso  : Set oFso = CreateObject("Scripting.FileSystemObject") 
Dim srcPath  : SrcPath = oFso.GetParentFolderName(WScript.ScriptFullName) 
Dim sWordList : sWordList = ofso.OpenTextFile(oFso.BuildPath(srcPath, "search.txt")).ReadAll() 
Dim sTargFile : sTargFile = ofso.OpenTextFile(oFso.BuildPath(srcPath, "target.txt")).ReadAll() 
Dim strWords : strWords = Join(Split(sWordList, vbCrLf), "|") 
Dim oReg  : Set oReg = New RegExp 
Dim oDict  : Set oDict = CreateObject("Scripting.Dictionary") 'for found words 
oDict.CompareMode = vbTextCompare 'case insensitive 

With oReg 
    .Global = True 
    .IgnoreCase = True 
    .Pattern = "("& strWords &")" ' (word1|word2|word3|etc..) 
    'if the words contain some special chars for regex, you may need to escape with \ char 
    'see the information below: http://goo.gl/cqGVp 
    Dim collFND : Set collFND = oReg.Execute(sTargFile) 
    Dim Fnd 
    'WScript.Echo String(50, "-") 
    For Each Fnd In collFND 
     With Fnd 
      'WScript.Echo "Word : "& .Value, ", Position: ("& .FirstIndex &","& .Length &")" 
      oDict(.Value) = "" 
     End With 
    Next 
    Set collFND = Nothing 
    'WScript.Echo String(50, "-"), vbCrLf, "Higlighted Output:", oReg.Replace(sTargFile, "[$1]") 
    ofso.CreateTextFile(oFso.BuildPath(srcPath, "foundwords.txt"),True).Write(Join(oDict.Keys, vbCrLf)) 'found words saved 
End With 
+0

嗨Kul-Tigin,再次感謝您的回覆,如何告訴這個腳本保存在文本文件中找到的單詞?還有什麼方法可以跳過消息嗎? – Nofuzy

+0

當然,我更新了答案。 –

+0

請參閱代碼中有關特殊字符的註釋。你應該逃脫特殊的字符。 –