2012-07-30 91 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