Regular Expression Object

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

 

REGULAR EXPRESSION:

Regular expressions are used to match patterns of text, enabling you to manipulate text easily. The regular expression object in VBScript is called RegExp, and has three properties, and three methods.

To use the RegExp object in Visual Basic, select "VBScript Regular Expressions" from the Project, References menu. The RegExp object is then instantiated using the New keyword, as in the following example.

Dim objRE As New RegExp
' When you've finished,
' release the object from memory
Set objRE = Nothing

The following table describes the Properties of the RegExp Object.

RegExp Object's Properties:

Property Example Description
Pattern objRE.Pattern = "[a-z]" The regular expression used for the match. The characters that define a regular expression are exaplained later in this tutorial. The pattern in the example matches the lowercase letters, a through to z.
IgnoreCase objRE.IgnoreCase = False A Boolean value to determine whether the match is case sensitive. By default, IgnoreCase is True.
Global objRE.Global = True A Boolean value to determine whether to match the first pattern in the string, or all possible matches. By default, Global is False.

The following table describes the Methods of the RegExp Object.

RegExp Object's Methods:

Method Description
Test The Test method returns True if the regular expression pattern is found in the string, otherwise it returns False. The following example matches the word "Stud" in "Juicy Studio", and outputs an "Expression found" message.

Dim objRE As New RegExp
nDim strExample As String
objRE.Pattern = "Stud"
strExample = "Juicy Studio"
' Test if a match is found
If objRE.Test(strExample) = True Then
     MsgBox "Expression found", vbInformation, "RegExp"
Else
     MsgBox "Expression not found", vbInformation, "RegExp"
End If
Set objRE = Nothing
Replace The Replace method returns a string with occurences of the regular expression pattern replaced with a new string. The replace method takes two arguments, the original string, and the pattern to replace. The following example replaces all occurences of "flat" to "juicy". The output is, "My juicy mate has a juicy tyre". If the Global property was not set in the example, the output would be, "My juicy mate has a flat tyre".

Dim objRE As New RegExp
Dim strExample As String
objRE.Pattern = "flat"
objRE.Global = True
strExample = "My flat mate has a flat tyre."
' Replace occurences of "flat" with "juicy"
strExample = objRE.Replace(strExample, "juicy")
MsgBox strExample, vbInformation, "RegExp"
Set objRE = Nothing
Execute The Execute method extends the functionality of the test method by returning a collection containing a Match object for each regular expression found in the string.

Dim objRE As New RegExp
Dim objMatches As MatchCollection
Dim objMatch As Match
Dim strExample As String
objRE.Pattern = "evil"
objRE.Global = True
strExample = "See no evil, hear no evil, speak no evil."
' Get all matches for evil
Set objMatches = objRE.Execute(strExample)
If objMatches.Count > 0 Then
     For Each objMatch In objMatches
          Debug.Print objMatch.Value & " found at position ";
          Debug.Print objMatch.FirstIndex
     Next
Else
     Debug.Print "The expression was not found"
End If
Set objMatches = Nothing
Set objRE = NothingThe above example returns the following:

evil found at position 7
evil found at position 21
evil found at position 36If the Global property was not set, only the first match at position 7 would be found in the search string.

PATTERN MATCHING:

The following table lists the special characters that may be used to match patterns with the regular expression object.

Special Characters:

Character Example Matches
\ objRE.Pattern = "\d" Used to find literal or special characters. In this example, the pattern matches a digit.
^ objRE.Pattern = "^Lemon" The match must occur at the beginning of the string. In the example, the pattern matches strings that start with the string, "Lemon".
$ objRE.Pattern = "Lemon$" The match must occur at the end of the string. In the example, the pattern matches strings that end with the string, "Lemon".
* objRE.Pattern = "L*" Matches zero or more times. In the example, the pattern matches a string of L's if they are present, otherwise an empty string.
+ objRE.Pattern = "L+" Matches one or more times. In the example, the pattern matches a string of L's. If no L's are present, a match will not be found.
? objRE.Pattern = "L?" Matches zero or one time. In the example, the pattern matches a single "L"if present, otherwise an empty string.
. objRE.Pattern = ".." Matches any character except new lines. In the example, the pattern matches the first two characters.
\b objRE.Pattern = "\bLe"
objRE.Pattern = "on\b"
Defines the boundary for the word. The first example matches any word in the expression that starts with "Le". The second example matches any word in the expression that ends with "on".
\B objRE.Pattern = "\BLe"
objRE.Pattern = "on\B"
Ensures the match is not on the boundary of a word. The first example matches any word in the expression that contains "Le", but doesn't start with "Le". The second example matches any word in the expression that contains "on", but doesn't end with "on".
\d objRE.Pattern = "\d" Used to match any single digit between 0 and 9. The example matches the first single digit in the expression.
\D objRE.Pattern = "\D" Used to match any non-digit. The example matches the first non-digit in the expression.
\s objRE.Pattern = "\s" Used to match any single white-space characters. The example matches the first white-space character.
\S objRE.Pattern = "\S" Used to match any single non white-space character. The example matches the first non white-space character.
\w objRE.Pattern = "\w" Used to match any character, digit or underscore. The example matches the first character, digit, or underscore.
\W objRE.Pattern = "\W" Used to match anything other than a character, digit or underscore. The example matches the first occurence of anything other than a character, digit, or underscore.
\xnn objRE.Pattern = "\x41" Used to match the ASCII character represented by the hexadecimal number nn. The ASCII character for 'A' is 65. The hexadecimal value is 41. The example matches the first occurence of an A.
[] objRE.Pattern = "[aeiou]" Used to match any of the enclosed characters.
[^] objRE.Pattern = "[^aeiou]" The first match that is not in the enclosed characters.
[c-c] objRE.Pattern = "[a-z]" Used to match a range of characters. The example matches the first lowercase character.
{n} objRE.Pattern = "w{3}" Used to match n occurences of the previous character. The example matches "www" if three or more W's are found together in the string.
{n,} objRE.Pattern = "w{3,}" Used to match at least n occurences of the previous character.If there were four w's in the example, it would match "wwww".
{m,n} objRE.Pattern = "w{3,5}" Used to match between m and n occurences of the previous character. The example tries to match between three and five w's.
() objRE.Pattern = "(em)" Brackets are used for grouping. The value found is stored, and may be used later for referencing. This technique is called backreferencing.
| objRE.Pattern = "a|e" Matches either of the character to the side of the operator. In the example, the first match of either an a or an e is found.

REGULAR EXPRESSION PATTERNS:

The following are example of some commonly used regular expression patterns.

Web Addresses or Emails:

The following example could be used to match URLs or Email Addresses.

objRE.Pattern = "^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$"

HTML TAGS

The following regular expression matches all tags in an HTML document. It can be combined with the Replace method to remove all tags from a document.

objRE.Pattern = "<\S[^>]*>"

 

The example can easily be extended to search for specific tags. The following example finds all image tags in a document.

objRE.Pattern = "<img\S[^>]*>"

DATES

The following regular expression may be used to validate a date.

objRE.Pattern = "^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}"

 

The following example of a RegExp object may be used to turn all tags brown in a RichTextFormatBox.

Dim objRE As New RegExp
Dim objMatch As Match
Dim objMatches As MatchCollection
Dim strXML As String
objRE.Pattern = "<\S[^>]*>"
objRE.Global = True
strXML = rtfXML.Text
If strXML <> "" Then
     Set objMatches = objRE.Execute(strXML)
     For Each objMatch In objMatches
          rtfXML.SelStart = objMatch.FirstIndex
          rtfXML.SelLength = objMatch.Length
          rtfXML.SelColor = RGB(128, 0, 0)
     Next objMatch
     Set objMatch = Nothing
     Set objMatches = Nothing
End If
Set objMatches = Nothing
Set objRE = Nothing

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641