简体   繁体   中英

VBA regex to find special characters in reverse

I'm trying to find special characters in a cell using the below code. However, still not getting desired out.

i created the regex in reverse, where it will allow few special characters and alpha numeric, as we don't know/have the special characters list.

 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

Is anything wrong with code or any other better way?

在此处输入图片说明

Assuming you want to return true if there are characters other than letters, digits and whitespace:

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

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

Explanation

                         EXPLANATION
--------------------------------------------------------------------------------
  [^0-9a-zA-                any character except: '0' to '9', 'a' to
  Z\s()[\]:'"/&%#!_+,.     'z', 'A' to 'Z', whitespace (\n, \r, \t,
  |-]                      \f, and " "), '(', ')', '[', '\]', ':',
                           ''', '"', '/', '&', '%', '#', '!', '_',
                           '+', ',', '.', '|', '-'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM