Solved

Regular Expressions & VB

Posted on 2000-03-31
5
302 Views
Last Modified: 2012-06-21
VBScript version 5 has a Regular Expression engine. Is there a way to access this functionality from within VB6?

How about a VBA-enabled app like Access2K?

thankx
--steve...

0
Comment
Question by:juststeve
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:VBGuru
ID: 2673241
it should be possible if it is a com component
0
 
LVL 6

Expert Comment

by:VBGuru
ID: 2673244
it should be possible if it is a com component
0
 
LVL 4

Accepted Solution

by:
Nazdor earned 50 total points
ID: 2673422
Assuming you have VBscript installed, in VB, simply add a reference to:

    Micrsoft VBScript Regular Expressions

(Project -> References)

Use the object browser (F2) for help on the regular expressions (look for VBScript_RegExp)

The help isn't very clear - basically you need to create a RegExp variable, then use "Test" to see if it's there.  If you want to see what the match is, then use Execute with Match and MatchCollection - but normally you just need to find if it's in there.

    Dim re As New RegExp

    re.IgnoreCase = True
    re.Pattern = "This is a test"

    On Error Resume Next
    If re.Test("Test") = False Then
        Text1 = "Not Found"
    Else
        Text1 = "Found!"
    End If
    On Error Goto 0


The 'On Error' is there because re.Test *will* cause some nasty problems if your pattern matching is corrupted (or, rather, the limited pattern matching doesn't understand what you want).

I've not found a definitive list of available regexps, but it doesn't like a lot of what I would consider 'standard' regexps.

0
 
LVL 4

Expert Comment

by:Nazdor
ID: 2673433
ooops, "text1" refers to a textbox on the form - use msgbox or whatever instead...  same with .Pattern and "Test".  
0
 

Author Comment

by:juststeve
ID: 2673628
Not sure if I'm really going blind or not....prior to posting my question I'd added a reference to VBScript Global - but couldn't instantiate a RegExp object. Didn't see the VBScript Regular Expressions right underneath. Interestingly, both references point to the same filename (vbscript.dll) but the RegExp appends a \2 ??

You may wish to visit another question i have going right now titled Editing hyperlinks. I have a feeling the solution to that question is going to be RegExp-centered.

For the benefit of others viewing this question...excellent starter artical on RegExp at:
http://msdn.microsoft.com/library/periodic/period99/VB99I1.HTM
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

15 Experts available now in Live!

Get 1:1 Help Now