Click here to Skip to main content
15,868,292 members
Articles / Web Development / IIS

Simplifed use of Regular Expressions from within your Scripts

Rate me:
Please Sign up or sign in to vote.
4.86/5 (7 votes)
27 Aug 2001CPOL1 min read 128.9K   757   40   4
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

VBScript
function rxTest( text, pattern, flags )

Use this function to test for a pattern inside a string. Acts similar to the

<a href=""http://www.perldoc.com/perl5.6/pod/perlre.html"">=~ s//</a>
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

VBScript
function rxReplace( text, pattern, replace_text, flags )

Use this function to replace a pattern inside a string with another string. Acts similar to the

<a href=""http://www.perldoc.com/perl5.6/pod/perlre.html"">=~ m//</a>
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

VBScript
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

<a href=""http://www.perldoc.com/perl5.6/pod/perlre.html"">=~ s//</a>
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

<a href=""http://msdn.microsoft.com/scripting/vbscript/doc/vscolmatches.htm"">Matches</a>
collection if a match is found, otherwise returns nothing.

Examples

  • Check whether a given URL starts with a certain protocol
    VBScript
    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!)
    VBScript
    dim blank
    blank = rxReplace( html, "<"&tag_name&"\b[^>]*>", "", "gi")
  • Replace all ocurrences of a certain syntax by a value, looked up from a database
    VBScript
    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:

VBScript
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)


Written By
Chief Technology Officer Zeta Software GmbH
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.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralThis is a _slow_solution Pin
31-Oct-01 3:49
suss31-Oct-01 3:49 
Generalexecuting a script Pin
Konstantin S. Diguine24-Jul-01 22:55
Konstantin S. Diguine24-Jul-01 22:55 
GeneralRe: executing a script Pin
Uwe Keim24-Jul-01 23:01
sitebuilderUwe Keim24-Jul-01 23:01 
Generalregex Pin
23-Jul-01 20:58
suss23-Jul-01 20:58 

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

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