• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3118
  • Last Modified:

Using RegExp of VBScript in VB6

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
kaspiton
Asked:
kaspiton
1 Solution
 
TimCotteeCommented:
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
 
EPol29Commented:
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
 
Dave_GreeneCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
kaspitonAuthor Commented:
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
 
TimCotteeCommented:
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
 
kaspitonAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now