Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Regular Expressions in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Regular Expressions in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-02-17

    Table of Contents:
  • Regular Expressions in VBScript
  • Searching and Replacing
  • Constructing Patterns
  • Building useful patterns

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Regular Expressions in VBScript - Building useful patterns


    (Page 4 of 4 )

    Now that you have all of the tools, let’s look at how to make them work.  Say we wanted to build a regular expression to match a standard ten-digit phone number in the form of (xxx) xxx-xxxx.

    The regular expression could easily begin as “(ddd) ddd-dddd”.  This is a string of literals: an opening parenthesis followed by three digits, a closing parenthesis, a space, three more digits, a hyphen, and the last four digits.

    If we apply repetition, we can condense this a bit to “(d{3}) d{3}-d{4}”.  Both expressions mean the same thing.  Now what if we wanted to make the parentheses optional?  Of course, if we do, the space should be a hyphen.  Enter grouping and alternation.

    To begin, we need to specify what it should look like with parentheses.  Thus our expression should begin with “(d{3) ” as before.  Now we want to add an alternate possibility in case parentheses aren’t used.  The expression then becomes “((d{3))|(d{3}-)”.  This will match three digits between parentheses followed by a space, OR three digits followed by a hyphen.  We then complete the expression by adding the remaining part of the match.  The final expression looks like “((d{3))|(d{3}-)d{3}-d{4}”.  This expression would match either “(123) 456-7890” or “123-456-7890.

    Another example would be a common U.S. zip code.  A U.S. zip code consists of five numeric digits followed by an optional four more separated by a hyphen.  Just as in the previous example, you can use grouping to accomplish this quite easily.  That expression would look like “d{5}(-d{4})?”.  Notice this time that I’m using the ? symbol to match either 0 or 1 of the last group.

    You can see that building regular expressions can provide very powerful tools for matching and replacing text strings.  Very complex expressions can be built using a simple set of character symbols.  Stayed tuned for a future article that demonstrates more advance uses of VBScript regular expressions.

    If you’re interested in testing your regular expressions, I’ve built a Regular Expression Tester HTML application using the regular expression test code found here.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek