RegExpr.Bas contains two functions that are used to match regular expressions (RegExpr) and get detailed results of the match (RegExprResult). You can use these functions by simply including RegExpr.Bas in your Visual Basic project.
RegExpr
Public Function RegExpr(Text, RegularExpression, Optional Flags) As Boolean
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 Regular expression syntax rules for detailed information about the syntax of the RegularExpression parameter.
RegExprResult
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. |
| 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. |
| etc. up to RegExprResult(31) | |
| 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(reMatches). |
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.
RegExprIndex
Public Sub RegExprIndex(ResultType, StartIndex As Long, Length As Long)
This is more or less an alternative to RegExprResult. RegExprIndex 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, Text$, NewValue$ Text$ = "setting = value" If RegExpr( Text$, "([a-zA-Z]+) = (.+)" ) Then ' Get the second subexpression, it's the value part RegExprIndex 2, StartIndex, Length NewValue$ = Mid(Text, StartIndex, Length) ' You could as well 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. |
| etc. up to 31. |
RegExprSubst
Public Function RegExprSubst(SubstituteString, Optional SubExpressionNr) As String
Substitutes strings that were matched during the previous call to function RegExpr. Returns the original string with part of it substituted with the value of SubstituteString. 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
RegExprSubst needs the previous call to RegExpr to be successful. RegExprSubst stores the substituted string, and successive calls to RegExprSubst, RegExprResult and RegExprIndex give results based on the substituted string. For example, 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
Word of 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.
Tutorial
Regular expression syntax rules
Subexpressions
Customizing RegExpr
Error codes