Windows Scripting
  Home arrow Windows Scripting arrow 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 / 5
    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


    (Page 1 of 4 )

    At this point in your scripting you’ve certainly come across VBScript’s InStr and Replace functions for searching and replacing within strings. While these string functions can come in pretty handy, they are also intrinsically limited. It’s time to step up into the Big Leagues and learn how to search and replace based on patterns.

    Like most other programming languages, VBScript uses regular expressions to perform pattern matching.  A regular expression is a textual expression that contains symbols and literal characters used to match a pattern of characters.  These can be extremely simple or scaled to be very complex, but if you take things one step at a time you’ll pick them up very easily.

    So when would you use regular expressions?  Well, for starters you can have much more powerful string replacements.  You might also use regular expressions to validate form entry in an HTML application or ASP page.  You’ll understand this better and realize the flexibility as you see some real-world examples.

    VBScript provides the RegExp object for creating and handling regular expressions.  It provides a series of methods and properties for searching and replacing based upon regular expression patterns.

    Set objRegExp = New RegExp

    The RegExp object is a native VBScript object.  However, it is not instantiated by default like most VBScript objects you’ve seen so far.  Thus it must be instantiated manually by referencing the RegExp class using a New statement.  A new statement simply returns the object instantiated by a specified class.

    objRegExp.Global = False

    objRegExp.IgnoreCase = True

    objRegExp.Multiline = False

    objRegExp.Pattern = "some pattern"

    The RegExp class provides several properties for controlling its behavior when matching or replacing.  The Global property is used to determine whether a RegExp replacement will replace all occurrences or only the first match found.  The IgnoreCase property accepts a Boolean value that indicates whether a match should be case sensitive.  The Multiline property is an undocumented property that indicates whether whitespaces should match line break characters.  By default, all three of these properties are set to False.  Finally, the Pattern property is a text string pattern that determines what matches are found.  You’ll learn more about constructing patterns in a bit.

    More Windows Scripting Articles
    More By Nilpo


     

    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-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek