Checking for Ascii Code using Regular Expressions in VBA

Posted on 2011-05-13
Last Modified: 2012-06-22
Hi Folks, Im using COM object VBScript Regular Expressions 5.5 - Currently I have the code for checking a string for a match but want to check for a ascii value instead, can this be done?
Sub Checkfile()
MsgBox (TestRegExp("is.", "isablea is1"))
End Sub
Function TestRegExp(myPattern As String, myString As String)
   'Create objects.
   Dim objRegExp As RegExp
   Dim objMatch As Match
   Dim colMatches   As MatchCollection
   Dim RetStr As String
   ' Create a regular expression object.
   Set objRegExp = New RegExp

   'Set the pattern by using the Pattern property.
   objRegExp.Pattern = myPattern

   ' Set Case Insensitivity.
   objRegExp.IgnoreCase = True

   'Set global applicability.
   objRegExp.Global = True
   'Test whether the String can be compared.
   If (objRegExp.Test(myString) = True) Then

   'Get the matches.
    Set colMatches = objRegExp.Execute(myString)   ' Execute search.

    For Each objMatch In colMatches   ' Iterate Matches collection.
      RetStr = RetStr & "Match found at position "
      RetStr = RetStr & objMatch.FirstIndex & ". Match Value is '"
      RetStr = RetStr & objMatch.Value & "'." & vbCrLf
    RetStr = "String Matching Failed"
   End If
   TestRegExp = RetStr
End Function

Open in new window

Question by:MrDavidThorn
    LVL 51

    Expert Comment

    use ASC("HainKurt") function : Asc returns the ANSI character code of the first letter of the string

    ASC("A") --> 65
    Chr(65) --> "A"

    Author Comment

    problem with Using ASC function is the way regular expression handles the match, i.e i want to search for Ascii code 160 which is a non-breaking space, with my current code, I cant pass the "   " and I cant pass the value "160" .
    LVL 51

    Accepted Solution

    could not get what this mean "I cant pass the value "160""

    in your pattern you can use "\xFF" or "\uFFFF" format

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
    Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now