Solved

Using RegExp of VBScript in VB6

Posted on 2001-07-17
6
2,986 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now