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