RegExpr tutorial

RegExpr for VB & VBA is a Visual Basic and Visual Basic for Applications code library for regular expression matching. Regular expressions are used for finding string patterns and string replacement. RegExpr brings the power of regular expressions to VB and VBA, which lack native support for them.

In order to use regular expressions, include RegExpr.Bas in your Visual Basic project. In VBA, include the RegExpr module in your document.

The RegExpr function

RegExpr will return True if a match is found and False otherwise. The simplest use for regular expressions is to check if a string matches a given pattern.

SomeText$ = "My name is Jesse James"
If RegExpr(SomeText$, "Jesse") Then ...

The condition is True if SomeText$ includes the string Jesse. By default, regular expressions are case sensitive, so "My name is jesse james" wouldn't match the above condition. You can set Flags = reCaseInSensitive to ignore case.

So far, all this could be easily done with VB's normal "=" or Like statements. Let's now see something more sophisticated.

Special characters

Here are some (but not all) useful special characters you can use in regular expressions:

.

Any single character (exception: see Flags)

*

Zero or more of the last character

+

One or more of the last character

?

Zero or one of the last character

Characters * + and ? are called quantifiers, because they are used to tell how many of something should be matched. You can use parentheses to group things. Here are some examples:

be.r

matches e.g. bear or beer

bee?

matches both be and bee

(bee)?

matches both bee and an empty string

gr+

matches gr, grr, grrr, grrrr etc.

argh*

matches arg, argh, arghh, arghhh etc.

(argh)+

matches argh, arghargh, argharghargh etc.

For example, you could use

SomeText$ = "The dog said grrrr"
If RegExpr(SomeText$, "gr+") Then RunAwayFast

Square brackets (character classes)

Square brackets are used to match any one of the characters inside them. It's somewhat similar to the [] syntax in VB's Like statement, but there are some differences.

[abc]

matches a single a, b, or c.

[abc]+

matches any combination of a, b and c.

A hyphen indicates "between" in ASCII order.

[a-c]

matches a single a, b, or c.

[c-a]

is a syntax error, because c comes after a in ASCII.

[a-zA-Z]

matches any lower- or upper-case character (but not including ASCII 128 - 255)