您可以使用\b
標記(用於單詞邊界)的模式在您希望的單詞前後進行正則表達式替換更換。
Public Function RegExpReplaceWord(ByVal strSource As String, _
ByVal strFind As String, _
ByVal strReplace As String) As String
' Purpose : replace [strFind] with [strReplace] in [strSource]
' Comment : [strFind] can be plain text or a regexp pattern;
' all occurences of [strFind] are replaced
' early binding requires reference to Microsoft VBScript
' Regular Expressions:
'Dim re As RegExp
'Set re = New RegExp
' with late binding, no reference needed:
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
re.Global = True
're.IgnoreCase = True ' <-- case insensitve
re.pattern = "\b" & strFind & "\b"
RegExpReplaceWord = re.Replace(strSource, strReplace)
Set re = Nothing
End Function
書面,搜索是區分大小寫的。如果你想不區分大小寫,使這條線:
re.IgnoreCase = True
在立即窗口...
? RegExpReplaceWord("one too three", "too", "two")
one two three
? RegExpReplaceWord("one tool three", "too", "two")
one tool three
? RegExpReplaceWord("one too() three", "too", "two")
one two() three
? RegExpReplaceWord("one too three", "to", "two")
one too three
? RegExpReplaceWord("one too three", "t..", "two")
one two three
...併爲您的分隔符的範圍......
? RegExpReplaceWord("one.too.three", "too", "two")
one.two.three
? RegExpReplaceWord("one,too,three", "too", "two")
one,two,three
? RegExpReplaceWord("one;too;three", "too", "two")
one;two;three
? RegExpReplaceWord("one:too:three", "too", "two")
one:two:three
? RegExpReplaceWord("one/too/three", "too", "two")
one/two/three
? RegExpReplaceWord("one?too?three", "too", "two")
one?two?three
? RegExpReplaceWord("one--too--three", "too", "two")
one--two--three
? RegExpReplaceWord("one***too***three", "too", "two")
one***two***three
的問題是你無法定義這些操作的規則。有太多的變化/排列。結果沒有一套邏輯能夠做到這一點。你所希望的最好的方法是標記需要人工評估的過程;檢查你想更新的那些,然後在手動審查後讓系統更新。否則,你將開發一個AI來處理permuatations。 – xQbert 2012-07-30 20:09:36
這不是真的,首先,MS已經做到了(你可以搜索整個單詞),第二,我可以想出一個組合列表,然後每個集合運行我的每個300字。我真的想盡量避免,但它是100%的可能。我可以做的另一件事是運行一個替換,將替換所有字符與空間,然後替換我的300個單詞,如果他們被空間包圍。所以有辦法做到這一點,我只是想找到做到這一點的最佳方式。我必須相信有辦法搜索整個單詞。謝謝!! – lalachka 2012-07-30 20:14:50
我已經有了代碼,可以給我9以下任何數字中的2的所有排列。我只是想盡量避免這種方式。 – lalachka 2012-07-30 20:17:45