Regular expressions in VBA, search for more than one ASCII Code

MrDavidThorn
MrDavidThorn used Ask the Experts™
on
Hi Folks, should be some easy points up for grabs here, Im using regular expressions 5.5 to search and replace a string based on ASCII codes, I have created a procedure that calls the regexe code and replaces the ASCII Code with whitespace, If I want to search and replace two codes I currently call the procedure twice, when Im guessing that I can somehow edit the pattern to search for two codes, any examples would be great.
Sub Checkfile()
Dim FSO As New FileSystemObject
Dim fsoFile As File
'Open an XML file to search for illegal characters
Set SourceFile = FSO.OpenTextFile("C:\TestWithInvalidCharcter.xml")
'Create a new XML file that would not contain new xml file
FSO.CreateTextFile ("C:\Test.xml")
fnum = FreeFile()
Open "C:\Test.xml" For Append As fnum

Do While Not SourceFile.AtEndOfStream
    'replace any ascii code 240 with whitespace and print to the file
    Print #fnum, TestRegExp("\240 040", SourceFile.ReadLine)
        'replace any ascii code 040 with white space and print to the file
    Print #fnum, TestRegExp("\040", SourceFile.ReadLine)
Loop
Close fnum


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 = objRegExp.Replace(myString, " ")
    Next
   Else
    RetStr = myString
   End If
   TestRegExp = RetStr
End Function

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
If I understand your question correctly, then I believe what you want is to use the vertical bar (meaning "OR"):
Print #fnum, TestRegExp("\240|\040", SourceFile.ReadLine)

Open in new window

Author

Commented:
thanks that works! - is there a AND condition as well?
Top Expert 2010

Commented:
MrDavidThorn,

Please explain what requirement you would have vis-a-vis an AND condition.  I should think that you would control this by passing in the proper pattern, but perhaps I misunderstand.

You may also want to have a look at my article on using RegExp in VBA and VB6:

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html

Patrick
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
thanks that works! - is there a AND condition as well?
You would need to concatenate both patterns and OR their permutations:
\240.*?\040|\040.*?\240

Open in new window

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Another, less tedious, approach would be to use a positive lookahead for each option. If you use this construct, then you only have to include each option once:
^(?=.*?\240)(?=.*?\040)

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial