Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simplifed use of Regular Expressions from within your Scripts

0.00/5 (No votes)
27 Aug 2001 1  
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 ""http://www.perldoc.com/perl5.6/pod/perlre.html"">=~ 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 ""http://www.perldoc.com/perl5.6/pod/perlre.html"">=~ 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 ""http://www.perldoc.com/perl5.6/pod/perlre.html"">=~ 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 ""http://msdn.microsoft.com/scripting/vbscript/doc/vscolmatches.htm"">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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here