Flag This Hub

How to use Regular Expressions (RegExp) in Visual Basic

By


Regular Expressions started off on Unix type operating systems. They are a neat way of matching strings. Regular Expressions are now available for use on a variety of programming languages, including Visual Basic 6.0!

Not many people know that Regular Expressions can easily be used with Visual Basic 6.0. But they are a neat way of performing string pattern matching and substitutions without having to use numerous Instr, Left, Right and Mid functions.

To get Regular Expressions working in Visual Basic 6.0, all you need to do is to include a reference to "Microsoft VBScript Regular Expressions 1.0" in your project. Simple as that!

Mastering Regular Expressions
Amazon Price: $25.47
List Price: $44.99
Learning Regular Expressions
Amazon Price: $19.59
List Price: $29.99

To test that your Visual Basic Regular Expressions are working, the following Visual Basic sample code will replace some HTML in a TextBox called TextBox1 with plain text:

Dim RegularExpressionObject As New VBScript_RegExp_10.RegExp

With RegularExpressionObject
.Pattern = "<[^>]+>"
.IgnoreCase = True
.Global = True
End With

TextBox1.Text = RegularExpressionObject.Replace(TextBox1.Text, "")

Set RegularExpressionObject = Nothing


Note that more recent versions of the VBScript Regular Expressions library also exist, so it may also be possible to make a reference to "Microsoft VBScript Regular Expressions 5.5". If this library is used then the first line of the Visual Basic code will have to be modified to:

Dim RegularExpressionObject As New VBScript_RegExp_55.RegExp

As well as using Regular Expressions to perform pattern substitutions in text, it is also possible to test for the presence of a specific Regular Expression.

There are a few useful properties of the Regular Expression object. The boolean IgnoreCase property allows for case insensitive pattern matching to be performed. The Global property specifies that the entire input string should be searched.

There are a large number of patterns that may be specified in the Pattern property of the Regular Expression. For example, the ^ character specifies that a match should be made at the start of a string, whereas the $ dollar sign represents a match at the end of a string. Most Unix-type Regular Expressions will work with the Visual Basic Regular Expression object.

Regular Expressions with the .NET Framework

Using Visual Basic.NET instead of Visual Basic 6.0? You'll be pleased to know that Regular Expressions are now an integral part of the Microsoft.NET Framework. Regular Expressions have their own class library in the .NET Framework - the namespace to look for is System.Text.RegularExpressions.

Mastering Regular Expressions
Amazon Price: $25.47
List Price: $44.99
Learning Regular Expressions
Amazon Price: $19.59
List Price: $29.99
Regular Expressions Cookbook
Amazon Price: $22.79
List Price: $44.99

Comments

tonystubblebine 17 months ago

Great post and thanks for including a link to the regex pocket reference (my book). I thought the .NET piece was the hardest chapter to write at first--I'd never done any windows programming. But I bought a copy of .NET and actually had a good time writing that chapter.

CelestialCoder 13 months ago

Excellent article!

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working