Back to help contents
Two functions, RegExpr and RegExprResult, let you match regular expressions and get detailed results of the matches. In order to use these functions, simply include RegExpr.Bas in your Visual Basic project. In VBA, include the RegExpr module in your document.
Public Function RegExpr(Text, RegularExpression, Optional Flags) As Boolean
RegExpr is the main function you need to call. RegExpr returns True if Text matches RegularExpression, and False otherwise.
The simplest use for regular expressions is to check if a string matches a given pattern. See the syntax rules for detailed information about what to put into the RegularExpression parameter.
Public Function RegExprResult(Optional ResultType) As String
After a successful call to RegExpr that returned True, you can call RegExprResult to find more information about the match. If RegExpr returned False, however, RegExprResult will raise an error (code RegExprError_NoMatch).
|RegExprResult( )||Returns the actual string that matched RegularExpression. Equivalent to $& in Perl.|
|RegExprResult(reMatch)||The same as RegExprResult( )|
|RegExprResult(rePreMatch)||Returns the string immediately before the matching string. Equivalent to $` in Perl.|
|RegExprResult(rePostMatch)||Returns the string immediately after the matching string. Equivalent to $' in Perl.|
|RegExprResult(1)||Returns the first matched subexpression. Equivalent to $1 in Perl.|
|RegExprResult(2)||Returns the second matched subexpression. Equivalent to $2 in Perl.|
|RegExprResult(31)||Returns the 31st matched subexpression.|
|RegExprResult(reMatches)||Returns the total number of subexpressions as a String. Usually, you need the number of subexpressions by looking at the regular expression, so you don't need to call RegExprResult|
At this point, you may want to read more about subexpressions, which are a useful way to match several parts of the input at the same time.
Public Sub RegExprIndex(ResultType, StartIndex As Long, Length As Long)
This Sub is an alternative to Function RegExprResult. RegExprIndex gives you the position and length of a match in the input string. As you call Sub RegExprResult, it sets StartIndex and Length corresponding to the value of ResultType. StartIndex and Length can be used in a Mid$ statement, like this:
Dim Startindex As Long, Length As Long Dim Text$, NewValue$ Text$ = "setting = value" If RegExpr( Text$, "([a-zA-Z]+) = (.+)" ) Then ' Get the 2nd subexpression, the value part RegExprIndex 2, StartIndex, Length NewValue$ = Mid$(Text, StartIndex, Length) ' Could also use NewValue$ = RegExprResult(2) End If
The ResultType parameter can be one of the following:
|reMatch||The string that matched RegularExpression. Equivalent to $& in Perl.|
|rePreMatch||The string immediately before the matching string. Equivalent to $` in Perl.|
|rePostMatch||The string immediately after the matching string. Equivalent to $' in Perl.|
|1||The first subexpression. Equivalent to $1 in Perl.|
|2||The second subexpression. Equivalent to $2 in Perl.|
|31||The 31st subexpression.|
Public Function RegExprSubst(SubstituteString, Optional SubExpressionNr) As String
This function is used for substituting a matched string with another string. RegExprSubst substitutes strings that were matched during the previous call to function RegExpr. RegExprSubst returns the original string with a part of it substituted with the value of SubstituteString.
Parameter SubExpressionNr works just like the ResultType parameter of RegExprIndex. The default is reMatch. Example:
If RegExpr("Result: Good", "Good|Excellent") Then MsgBox RegExprSubst("Accepted") ' Displays Result: Accepted End If
First call RegExpr to match something, then call RegExprSubst to sustitute something.
RegExprSubst stores the substituted string. Successive calls to RegExprSubst, RegExprResult and RegExprIndex give results based on the previously substituted string. You can call RegExprSubst successively like this:
If RegExpr("Result: Good", "Good|Excellent") Then RegExprSubst "The result was ", rePreMatch RegExprSubst "!!!", rePostMatch MsgBox RegExprSubst("accepted") ' Displays The result was accepted!!! End If ' Or maybe you wanted to hide someone's ' login name and password: If RegExpr("login: mylogin password: mypwd", _ "login: ([a-z]+) password: ([a-zA-Z0-9]+)") Then RegExprSubst "yourlogin", 1 MsgBox RegExprSubst("*******", 2) ' Displays login: yourlogin password: ******* End If
Warning: When subexpressions overlap with substituted parts, the contents of the subexpressions are undefined in successive calls to RegExprResult, RegExprIndex and RegExprSubst. This is because the substitution overwrites text that was originally matched as a subexpression.
Regular expression syntax rules