Click here to Skip to main content
Click here to Skip to main content
Go to top

Simplifed use of Regular Expressions from within your Scripts

, 27 Aug 2001
Rate this:
Please Sign up or sign in to vote.
Small library to make working with Regular Expressions a little bit easier

Introduction

Knowing the Regular Expression-capabilities and -syntax of Perl, I wanted to have a similar syntax from within my VBScript code. VBScript uses the RegExp object, so I wrapped the calls to the RegExp object inside three function.

Functions

rxTest

function rxTest( text, pattern, flags )

Use this function to test for a pattern inside a string. Acts similar to the =~ s// operator in Perl.

The arguments are:

  • text
    The text string upon which the regular expression is executed.
  • pattern
    Regular string expression being searched for.
  • flags
    i: case insensitive, g: global, m: multiline

Returns true if found, false if not found.

rxReplace

function rxReplace( text, pattern, replace_text, flags )

Use this function to replace a pattern inside a string with another string. Acts similar to the =~ m// operator in Perl.

The arguments are:

  • text
    The text string upon which the regular expression is executed.
  • pattern
    Regular string expression being searched for.
  • replace_text
    The replacement text string.
  • flags
    i: case insensitive, g: global, m: multiline

Returns the modified string. The input-string is not modified by this function.

rxExec

function rxExec( text, pattern, flags )

Use this function to test for a pattern inside a string and return the matched elements. Acts similar to the =~ s// operator in Perl.

The arguments are:

  • text
    The text string upon which the regular expression is executed.
  • pattern
    Regular string expression being searched for.
  • flags
    i: case insensitive, g: global, m: multiline

Returns a Matches collection if a match is found, otherwise returns nothing.

Examples

  • Check whether a given URL starts with a certain protocol
    dim is_absolute
    is_absolute = rxTest( url, "^(https|http|ftp|mailto|javascript|jscript)://", "gis" )
  • Remove a certain HTML tag from a string (stupid simple version!)
    dim blank
    blank = rxReplace( html, "<"&tag_name&"\b[^>]*>", "", "gi")
  • Replace all ocurrences of a certain syntax by a value, looked up from a database
    do
      set matches = rxExec(html, "oid://([0-9]+)", "g")
    
      if not IsNothing(matches) then
        if matches.Count>0 then
          set match = matches(0)
    
          dim replacement
          if match.SubMatches.Count>0 then
            replacement = GetNameFromDatabaseByID(match.SubMatches(0))
          else
            replacement = ""
          end if
    
          html = Replace(html, match.Value, replacement)
        else
          exit do
        end if
      else
        exit do
      end if
    loop

The last example uses the function IsNothing which is defined as followed:

function IsNothing( byref obj )
	IsNothing = CBool(LCase(TypeName(obj))="nothing")
end function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.
 
In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
 
Some cool, free software from us:
 
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralThis is a _slow_solution PinmemberAnonymous31-Oct-01 3:49 
Generalexecuting a script PinmemberKonstantin S. Diguine24-Jul-01 22:55 
GeneralRe: executing a script PinmemberUwe Keim24-Jul-01 23:01 
Generalregex PinmemberAnonymous23-Jul-01 20:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 28 Aug 2001
Article Copyright 2001 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid