【发布时间】:2024-01-02 08:48:01
【问题描述】:
我正在尝试使用 Excel VBA 中的以下 RegEx 删除所有不可打印和非 ASCII(扩展)字符:
[^\x09\0A\0D\x20-\xFF]
理论上,这应该匹配除制表符、换行符、回车符或可打印的 ASCII 字符(十六进制 20 和 FF 或 dec 32 和 255 之间的字符代码)之外的任何内容。我已经确认here Microsoft VBScript 正则表达式支持 \xCC 表示法,其中 CC 是十六进制的 ASCII 代码。
问题是这个正则表达式匹配超过 127 的每个字符。然后当匹配字符的代码高于 127 时,它会在 match.value 上抛出一个“无效的过程调用”。仅仅是 VBScript RegExes 不支持字符代码吗127以上?我似乎无法在任何地方找到这些数据。完整代码如下:
regEx.Pattern = "[^\x09\0A\0D\x20-\xFF]"
regEx.IgnoreCase = True 'True to ignore case
regEx.Global = True 'True matches all occurances, False matches the first occurance
regEx.MultiLine = True
If regEx.Test(Cells(curRow, curCol).Value) Then
Set matches = regEx.Execute(Cells(curRow, curCol).Value)
numReplacements = numReplacements + matches.Count
For matchNum = matches.Count To 1 Step -1
Cells(numReplacements - matchNum + 2, 16).Value = matches.Item(matchNum).Value
Cells(numReplacements - matchNum + 2, 17).Value = Asc(matches.Item(matchNum).Value)
Next matchNum
Cells(curRow, curCol).Value = regEx.Replace(Cells(curRow, curCol).Value, replacements(pattNo))
End If
它匹配的第一个字符是 0x96 (&ndash)。当我观看“比赛”并展开它时,我可以在“观看”窗口中看到它。但是,当我尝试观看 match.Item(matchNum).Value 时,我得到了(见截图)。有什么想法吗?
【问题讨论】:
-
应该是[^\x09\x0A\x0D\x20-\xFF]?
-
我也认为 MatchCollection 是从零开始的。 (msdn.microsoft.com/en-us/library/…)
标签: regex excel vba vbscript non-printable