2012-07-30 44 views
3

我正在尋找一種方法來搜索和替換整個單詞。因爲在我的情況下,整個單詞不僅可以用空格隔開,而且......,/?等我想不出一種有效的方式來編碼。只搜索並替換全文

基本上,我希望做這樣的事情

replace([address], ***--list of separators, like .,;:/?--*** & [replacewhat] & ***--list of separators, like .,;:/?--*** ," " & [replacewith] & " ") 

我不知道如何來一次,而不是運行一次分離的每個組合替換功能通過分離器的列表(其中合併與我替換的300個單詞將相當於一個瘋狂的查詢數量)

+0

的問題是你無法定義這些操作的規則。有太多的變化/排列。結果沒有一套邏輯能夠做到這一點。你所希望的最好的方法是標記需要人工評估的過程;檢查你想更新的那些,然後在手動審查後讓系統更新。否則,你將開發一個AI來處理permuatations。 – xQbert 2012-07-30 20:09:36

+2

這不是真的,首先,MS已經做到了(你可以搜索整個單詞),第二,我可以想出一個組合列表,然後每個集合運行我的每個300字。我真的想盡量避免,但它是100%的可能。我可以做的另一件事是運行一個替換,將替換所有字符與空間,然後替換我的300個單詞,如果他們被空間包圍。所以有辦法做到這一點,我只是想找到做到這一點的最佳方式。我必須相信有辦法搜索整個單詞。謝謝!! – lalachka 2012-07-30 20:14:50

+0

我已經有了代碼,可以給我9以下任何數字中的2的所有排列。我只是想盡量避免這種方式。 – lalachka 2012-07-30 20:17:45

回答

12

您可以使用\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 
+0

我做錯了什麼?我在查詢Expr2中使用這個:RegExpReplaceWord([newaddress1],「st」,「street」),它不會被替換。 NewAddress1 \t Expr2 1 COLUMBIA ST 1 COLUMBIA ST – lalachka 2012-07-30 21:36:58

+0

ohh,區分大小寫,沒問題,這是如此優雅!!)))))) – lalachka 2012-07-30 21:40:06

+0

好吧,我道歉,我是新來的這個網站,並沒有知道事情如何工作。你的兩個解決方案都是答案,但我試圖將你的答案標記爲答案,並將其從Remou's中解救出來。我想給你兩個信用,這是如何完成的?我已經1升了你,這足夠嗎? PS哦,嗨HansUp)))))我記得你從ShellAndWait和你的WTF條款)))))))再次感謝你 – lalachka 2012-07-30 21:42:42