Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 识别特殊字符_Regex_Excel_Vba - Fatal编程技术网

Regex 识别特殊字符

Regex 识别特殊字符,regex,excel,vba,Regex,Excel,Vba,我需要识别具有某些特殊字符(例如:!,.=]\')的单元格,并用颜色标记它们 该列只能包含数字(0-9)、字母(a-z)、大写字母(a-z)和连字符(-) 示例:您可以使用正则表达式执行此任务 这里一个有用的正则表达式构造是:使用[^…]并在其中插入不希望匹配的范围。因此,要匹配ASCII字母、数字和连字符以外的字符,请使用[^a-zA-Z0-9-] 像这样使用它 Dim strPattern As String: strPattern = "[^a-z0-9-]" Dim regEx As O

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

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


示例:

您可以使用正则表达式执行此任务

这里一个有用的正则表达式构造是:使用
[^…]
并在其中插入不希望匹配的范围。因此,要匹配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
Dim strPattern As String:strPattern=“[^a-z0-9-]”
Dim正则表达式作为对象
设置regEx=CreateObject(“VBScript.RegExp”)
regEx.Global=True
regEx.IgnoreCase=True
regEx.Pattern=strPattern
对于ActiveSheet.Range(“C:C”)中的每个单元格,请在此处定义您自己的范围
如果strPattern为“”,则如果单元格不为空,则为“”
如果是regEx.Test(cell.Value),则“检查是否存在匹配项”
cell.Interior.ColorIndex=6'如果是,请更改背景色
如果结束
如果结束
下一个
不带正则表达式:

此宏处理列B

Sub marine()
    Dim r As Range, rng As Range, s As String
    Dim i As Long, L As Long

    Set rng = Intersect(Range("B:B"), ActiveSheet.UsedRange)

    For Each r In rng
        If r.Value <> "" Then
            s = Replace(r.Text, "-", "")
            L = Len(s)
            For i = 1 To L
                If Not Mid(s, i, 1) Like "[0-9a-zA-Z]" Then
                    r.Interior.Color = vbYellow
                End If
            Next i
        End If
    Next r
End Sub
Sub-marine()
调暗r为范围,rng为范围,s为字符串
我一样长,我一样长
设置rng=Intersect(范围(“B:B”),ActiveSheet.UsedRange)
对于rng中的每个r
如果r.值为“”,则
s=替换(r.Text,“-”,“”)
L=Len(s)
对于i=1到L
如果不是中间(s,i,1)像“[0-9a-zA-Z]”那么
r、 内饰。颜色=黄色
如果结束
接下来我
如果结束
下一个r
端接头

它只接受数字、大写字母和小写字母以及破折号。

Regex->似乎在字母和数字旁边,单元格也可以有
-
,对吗?也可以在没有VBA宏和条件格式公式的情况下进行,为什么要进行向下投票?有人能解释一下吗?你回答的反对票让我困惑,所以我+1,因为我看不到任何问题。请给出任何提示,说明为什么这个答案没有用。@Slai you可能是某个地方的一个bug…………我会重新检查答案。我没有否决你的答案,谢谢你的支持time@Deluq如果我在没有解释的情况下发布代码,我通常会得到反对票……谢谢你的反馈!是的,事实上我喜欢你没有regext的答案上面的代码很好。。我需要特殊的角色count@VinothNarayan这是一个新问题吗?实际上,这太简单了:对于每个块,只需使用
设置ms=regEx.Execute(cell.Value)
然后使用
ms.Count
,而不是整个