VBA正则表达式反向查找特殊字符

时间:2020-09-15 17:33:39

标签: excel regex vba

我正在尝试使用以下代码在单元格中查找特殊字符。但是,仍然没有得到满足。

我反向创建了正则表达式,因为我们不知道/没有特殊字符列表,所以它只允许少量特殊字符和字母数字。

Function spltrack(cell As String)

'Use Regular Expressiosn for grabbing the input and automatically filter it
Dim regEx As New RegExp

With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    
    .Pattern = "[0-9][a-zA-Z]|(\[\]())|.+-/|'&%#_:,!–"
End With

If regEx.Test(cell) Then
    spltrack = "Not Found"
Else
    spltrack = "Found"
End If


End Function

代码或其他更好的方法有什么问题吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

假设您要返回 true ,如果除了字母,数字和空格之外还有其他字符:

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv='cache-control' content='no-cache'>
    <meta http-equiv='expires' content='-1'>
    <meta http-equiv='pragma' content='no-cache'>

    <script type="text/javascript">
        function myFunction() {
            var ajaxRq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
            ajaxRq.open("GET", "data.txt", false);
            ajaxRq.send(null);
            document.write(ajaxRq.responseText);
        };
    </script>
</head>

<body>

<pre><script>myFunction();</script></pre>

</body>
</html>

说明

With regEx
    .Pattern = "[^0-9a-zA-Z\s()[\]:'""/&%#!_+,.|-]"
End With

If regEx.Test(cell) Then
    spltrack = "Found"
Else
    spltrack = "Not Found"
End If