Solved

Using RegExp of VBScript in VB6

Posted on 2001-07-17
6
2,993 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
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.

 

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

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

Suggested Solutions

Title # Comments Views Activity
Run code from text file in vb 1 56
Hide vba in gp 7 82
Recommendation vb6 to vb.net or others 14 109
VB to send out individual emails works only if Outlook is running. 8 40
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now