When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, September 01, 1999

An Introduction to Regular Expression with VBScript, Part 2


  • Read Part 1

  • In Part 1 we discussed the "rules" of regular expression. In this article I will show you how to use the regular expression object inherent in VBScript 5. (If you do not have the VBScript 5 Scripting engine installed, refer to Part 1 for installation instructions.)

    - continued -

    First things first: you need to create an instance of the regexp object. This can be accomplished with the following code:

    'Always do this: (good programming practice!) Option Explicit 'Declare our variable Dim objRegExpr 'Create an instance of the regexp object Set objRegExpr = New regexp

    There are a couple of nifty properties in the regexp object. One such property is the Pattern property. We set this property equal to our regular expression. So, let's say that we are wanting to search a string for a valid phone number (a very common regular expression-tutorial question). Let's say that all phone numbers will have this form:

    (XXX)XXX-XXXX

    Note that there are no spaces in that string. So, our regular expression could look like:

    \([0-9]{3}\)[0-9]{3}-[0-9]{4}

    Note: We need the backslashes before the parenthesis because the parenthesis are special characters (remember, they are used for grouping!)...

    That says that we want to match strings that have a left parenthesis, three characters between zero and nine, a right parenthesis, three characters between zero and nine, a dash, and then four characters between zero and nine. Only strings that match this strict guideline will be found with our regular expression search.

    Another property, IgnoreCase, which is false by default, determines if we want our search to be case sensitive. The final property of the regexp object is Global, which determines if a regular expression should search the entire string for multiple matches, or if it should just be satisfied with the first match it finds. Multiple matches are searched for when Global is true, and only match is searched for if Global is false.

    So, let's set these properties:

    objRegExpr.Pattern = "\([0-9]{3}\)[0-9]{3}-[0-9]{4}" objRegExpr.Global = True objRegExpr.IgnoreCase = True

    The regexp object has three methods, but I am only going to focus on one of them right now. The Execute method searches for the pattern specified by the Pattern property in the string it is passed. It returns a collection of Matches. So, let's define the string that we want to use our regular expression to search on:

    'What string are we searching on? Dim strSearchOn strSearchOn = "My phone number is (123)654-3211. Sue's phone " & _ "number is (873)392-1222." 'Declare a variable to hold our collection of Matches Dim colMatches 'Now, Execute the regular expression search Set colMatches = objRegExpr.Execute(strSearchOn)

    The above code creates the string we are going to search (strSearchOn), and executes the search. The Execute method returns a collection of Matches, which we assign to our variable colMatches. You may be wondering what a Match is. Well, it is another object that represents all of the strings we found. In our example, we would have found two matches, "(123)654-3211" and "(873)392-1222".

    Now, let's say that we want to display all of the matching strings... easy enough, we just need to step through the Matches collection and display the each match!

    Dim objMatch 'Print the # of matches we found Response.Write colMatches.Count & " matches found...<P>" 'Step through our matches For Each objMatch in colMatches Response.Write objMatch.Value & "<BR>" Next 'Clean up Set colMatches = Nothing Set objRegExpr = Nothing

    The above code will print out the number of matches we found, as well as each match.

    Well, that's it! I hope you've learned a lot and are now excited about using regular expressions in your code! I will definitely follow this article up with other, related articles, so stay tuned! In closing, let me highly recommend that you check out Microsoft Beefs up VBScript with Regular Expressions, an article available on Microsoft's site.

    Happy Programming!

  • By Scott Mitchell


  • Read Part 1


    Attachments:

  • Source code to the example in this article in text format...


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES