【发布时间】:2013-04-16 13:27:21
【问题描述】:
我有一个原始单词列表,并替换为我想替换某些句子中原始单词出现的单词。
例如我的清单:
theabove the above
myaddress my address
所以句子“This is theabove”。将变为“这是上面的。”
我在 VB 中这样使用正则表达式:
Dim strPattern As String
Dim regex As New RegExp
regex.Global = True
If Not IsEmpty(myReplacementList) Then
For intRow = 0 To UBound(myReplacementList, 2)
strReplaceWith = IIf(IsNull(myReplacementList(COL_REPLACEMENTWORD, intRow)), " ", varReplacements(COL_REPLACEMENTWORD, intRow))
strPattern = "\b" & myReplacementList(COL_ORIGINALWORD, intRow) & "\b"
regex.Pattern = strPattern
TextToCleanUp = regex.Replace(TextToReplace, strReplaceWith)
Next
End If
我将列表 myReplacementList 中的所有条目与我要处理的文本 TextToReplace 进行循环,并且替换必须是整个单词,因此我在原始单词周围使用了“\b”标记。
效果很好,但是当原始单词包含一些特殊字符时我遇到了问题,例如
overla) overlay
我试图逃避模式中的 ) 但它不起作用:
\boverla\)\\b
我不能用那个词替换句子“这个词是重叠的”。到“这个词与那个词重叠。”
不确定缺少什么?正则表达式是上述场景的方法吗?
【问题讨论】: