regex - 识别特殊字符

标签 regex excel vba

我需要识别具有某些特殊字符(例如:!,.=]\')的单元格并用颜色标记它们。

该列只能包含数字 (0-9)、字母 (a-z)、大写字母 (A-Z) 和连字符 (-)。

示例:enter image description here

最佳答案

您可以使用正则表达式来完成此任务。

这里一个有用的正则表达式构造是 negated character class :您使用 [^...] 并在其中插入您不想匹配的范围。因此,要匹配 ASCII 字母、数字和连字符以外的字符,请使用 [^a-zA-Z0-9-]

并像这样使用它

Dim strPattern As String: strPattern = "[^a-z0-9-]"
Dim regEx As Object

Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = strPattern

For Each cell In ActiveSheet.Range("C:C") ' Define your own range here
    If strPattern <> "" Then              ' If the cell is not empty
        If regEx.Test(cell.Value) Then    ' Check if there is a match
            cell.Interior.ColorIndex = 6  ' If yes, change the background color
        End If
    End If
Next

关于regex - 识别特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311253/

相关文章:

vba - 更改 Excel ActiveX 列表框的名称

excel - VBA getelementsbytagname问题

excel - 当工作簿 1 中的单元格值与工作簿 2 中的列值匹配时,将值从工作簿 2 复制到工作簿 1(主工作簿)

excel - 使用 vba 中的列号创建 Excel 范围?

Python将 'N'字节数据写入二进制文件

javascript正则表达式替换水平椭圆

Python 正则表达式 : Formatting use of commas, 国际句点

python - 正则表达式-匹配之间捕获文本

ruby - 使用 Ruby 获取网页的所有链接

vba - HTTP 请求在我运行程序时卡住,但在我介入时没有卡住