Solved

Using RegExp of VBScript in VB6

Posted on 2001-07-17
6
3,012 Views
Last Modified: 2012-06-21
Hello!

In VBScript i can create and use Regular Expression object like that:

'Creates a regexp object
dim regEx
set regEx = New RegExp

and how can i use this object in VB6?
What reference library i must to add in my project or
something else?

thank you!
0
Comment
Question by:kaspiton
6 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 40 total points
ID: 6290039
You need to add the Microsoft VBScript Regular Expressions 5.5 library to your references in order to be able to use these objects.
0
 

Expert Comment

by:EPol29
ID: 6290185
After setting up reference ( see prev.answer) - create a wrapper class.
Following is my example of the wraper class to use VBScript Regular expressions:
 
******************************************
Class module  - CRegExpr
**************

Option Explicit

Public Function reReplace(a_Patrn As String, ByVal a_InputStr As String, a_replStr As String)
  Dim regEx As RegExp
  Dim lInpStr As String                 ' Create variables.
  Dim lRetString As String
 
  lInpStr = a_InputStr
  Set regEx = New RegExp                ' Create regular expression.
  regEx.Pattern = a_Patrn          ' Set pattern.
  regEx.IgnoreCase = True               ' Make case insensitive.
  If reTest(a_Patrn, a_InputStr) Then
      lRetString = regEx.Replace(lInpStr, a_replStr)   ' Make replacement.
   Else
      lRetString = a_InputStr
  End If
    reReplace = lRetString
End Function
Public Function reFindVal(a_Patrn As String, _
                    ByVal a_SearchStr As String, _
                          a_NoMatch As String) As String
  Dim Retval As String
  Dim regEx As RegExp
  Dim lMatches As MatchCollection
  Dim lMatch As match
 
  Set regEx = New RegExp            ' Create regular expression.
  regEx.Pattern = a_Patrn         ' Set pattern.
  regEx.IgnoreCase = True           ' Set case insensitivity.
  regEx.Global = True                 ' Set global applicability.
 
  Set lMatches = regEx.Execute(a_SearchStr)    ' Execute search.
 
  For Each lMatch In lMatches     ' Iterate Matches collection.
 
      Retval = lMatch.Value
      Exit For
   Next
   
   If Len(Retval) = 0 Then
      Retval = a_NoMatch
   End If
 
    reFindVal = Retval
 
End Function

Public Function reFindPos(a_Patrn As String, ByVal a_SearchStr As String) As Long
  Dim lRetPos As Long
  Dim regEx As RegExp
  Dim lMatches As MatchCollection
  Dim lMatch As match
  Set regEx = New RegExp            ' Create regular expression.
  regEx.Pattern = a_Patrn        ' Set pattern.
  regEx.IgnoreCase = True           ' Set case insensitivity.
  regEx.Global = True           ' Set global applicability.
  Set lMatches = regEx.Execute(a_SearchStr)    ' Execute search.
 
 
 
  For Each lMatch In lMatches     ' Iterate Matches collection.
      lRetPos = lMatch.FirstIndex
      Exit For
  Next
  reFindPos = lRetPos
End Function
Public Function reFindStr(patrn, strng) As String
  Dim RetStr As String
  Dim regEx, match, Matches     ' Create variable.
  Set regEx = New RegExp            ' Create regular expression.
  regEx.Pattern = patrn         ' Set pattern.
  regEx.IgnoreCase = True           ' Set case insensitivity.
  regEx.Global = True           ' Set global applicability.
  Set Matches = regEx.Execute(strng)    ' Execute search.
 
 
  For Each match In Matches     ' Iterate Matches collection.
    RetStr = RetStr & "Match found at position "
    RetStr = RetStr & match.FirstIndex & ". Match Value is " '
    RetStr = RetStr & match.Value & "'." & vbCrLf
  Next
  reFindStr = RetStr
End Function

Public Function reTest(patrn, strng) As Boolean
  Dim regEx, Retval             ' Create variable.
  Set regEx = New RegExp            ' Create regular expression.
  regEx.Pattern = patrn         ' Set pattern.
  regEx.IgnoreCase = False      ' Set case sensitivity.
  Retval = regEx.Test(strng)        ' Execute the search test.
  If Retval Then
    reTest = True
  Else
    reTest = False
  End If
End Function

*************************************************

Good luck!

Eva
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6290194
Globally replace text using just 5 lines of Visual Basic code

If you've ever tried to implement a text search and replace function in Visual Basic, you probably resorted to lengthy parsing loops that used Instr() and an entire host of string functions. If so, you'll be glad to know that there's a much easier way.

With the advent of VBScript 5.0, Microsoft introduced the Regular Expression engine, which you can also use in Visual Basic. If you've used Perl or JavaScript, you may be familiar with these pattern-matching powerhouses. In a nutshell, regular expressions let you define a pattern, literal or representative, which you can then match against a second string. For example, the literal pattern 'abc' as a regular expression would find a match in 'dabcef', 'abcdef', or 'defabc'. To further refine a regular expression, the Regular Expression engine offers a host of special metacharacters, similar to wildcard characters. For example, using these metacharacters, you could search for any word that began with a letter and ended in a digit. To view these metacharacters and their uses, visit Microsoft Windows Script Technologies.

then search in the VBScript documentation for the RegExp object's Pattern property.

To create a simple find and replace subroutine, first download the VBScript DLL from http://www.microsoft.com/msdownload/vbscript/scripting.asp.

Once you've registered the dll on your system, in your Visual Basic project set a reference to Microsoft VBScript Regular Expressions. Now, via code, create a RegExp object, like so

Set MyReg = new RegExp

Next, set the object's properties.

MyReg.IgnoreCase = True
MyReg.Global = True
MyReg.Pattern = "abc"

Here, we've used a literal string to create a global, case-insensitive pattern. Finally, you execute the object's Replace method on the target string, as in

txtSearchText = MyReg.Replace(txtSearchText, "def")

That's *all* there is to it! This example would replace "abc" with "def" wherever it occurred within txtSeartText. To execute the replace only on the first matching string, set the Global property to False.

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:kaspiton
ID: 6290212
I write function - ClearHTMLTags

I use it in some DLL.
When i use this function it make error like:
"ariable uses an Automation type not supported in Visual Basic"

what is the problem?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6290242
Eva, you are new to this site so are understandably unaware of the protocol. Please read the guidelines on comments vs answers at the bottom of this page and refrain from posting answers in this way. Thank you at least acknowledging my previous post.

I didn't post any more information on the basis that the questioner implied that he/she already was familiar with regular expressions in vbscript so there was little point.

kaspiton, is this a different question? If so really it should have been posted as a seperate question. In essence however I would assume that the dll was written in something other than VB and therefore does not expose the appropriate interface(s) for it to work in VB.
0
 

Author Comment

by:kaspiton
ID: 6290260
I write function - ClearHTMLTags

I use it in some DLL.
When i use this function it make error like:
"ariable uses an Automation type not supported in Visual Basic"

what is the problem?
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
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…

821 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