Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create a better InStr function with VBScript regular expressions

Total Hit ( 15299)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Regular expressions offer an extremely powerful way to search and replace text inside a string using sophisticated search criteria, far behind the capabilities of VB's InStr and Replace functions. For example, you can search for whole words, or find a match with a range of characters (for example, a digit or any uppercase character), and can quickly locate and count all the occurrences of the searched string. Regular expressions are so important for many programming tasks that there are at least a couple of programming languages - Awk and Perl - whose main purpose is making regular expressions available to developers.

If you've installed VBScript version 5 or later - which is automatically installed with any version of Microsoft Internet Explorer starting at version 4.0 - you can take advantage of the powerful regular expression engine that comes with it. This engine is available to all VBScript developers - for example, from within Windows Script Host and Active Server Pages applications - and is even available to Visual Basic developer. All you have to do to use this regular expression engine from within your VB apps is add a reference to the Microsoft VBScript Regular Expressions type library.

The main object in the type library is the RegExp object, which represents a regular expression pattern. A pattern identifies the text you are searching for. You can use several special characters in the pattern to search for a category of characters (e.g. a symbol or a non-digit) or to specify where the searched string should appear in the text (e.g. at the beginning of a word, or just before the end of the line). To put regular expressions to good use you must learn how to use these special sequences of characters, a job that can be quite daunting and far beyond the scope of this simple tip, that is meant to work just an introduction to using the regular expression type library.

The main property of the RegExp object is Pattern, to which you assign the regular expression pattern soon after creating the object. You can also set the IgnoreCase property to True or False, depending on whether you want to perform case insensitive searches. Another important property is Global, that should be set to True if you want to find all the occurrences of the pattern, or False if you just want to test whether the pattern is contained at least once in the source string. Finally, the Execute method takes the source string - that is, the string inside which you're searching the pattern - and returns a collection of Match objects. Each Match object represents an occurrence of the pattern, and exposes properties such as Value (the occurrence found), Length (number of characters in the occurrence), and FirstIndex (the position of the occurrence in the source string).

As an example of how to use the RegEx object, the following code searches all the occurrences of an uppercase character followed by three digits:

Click here to copy the following block
' NOTE: this code requires a reference to the
'    Microsoft VBScript Regular Expression type library
'
Dim re As New RegExp
Dim ma As Match

re.Pattern = "[A-Z][0-9][0-9][0-9]"  ' uppercase char followed by 2 digits
re.IgnoreCase = False         ' case sensitive search
re.Global = True            ' find all the occurrences

For Each ma In re.Execute(txtSource.Text)
  Print "Found '" & ma.Value & "' at index " & ma.FirstIndex
Next

Because the special \d reqular expression sequence means "a digit character", the Pattern property in the above example could be assigned also as follows:

Click here to copy the following block
re.Pattern = "[A-Z]\d\d\d"

For additional information about the syntax of regular expressions that you can use with the RegEx object, please see the documentation that comes with VBScript and make a search on MSDN Online.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.