Solved

Regular Expressions & VB

Posted on 2000-03-31
5
300 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

13 Experts available now in Live!

Get 1:1 Help Now