5,276,406 members and growing! (17,864 online)
Email Password   helpLost your password?
Web Development » ASP » Components     Intermediate License: The Code Project Open License (CPOL)

Simplifed use of Regular Expressions from within your Scripts

By Uwe Keim

Small library to make working with Regular Expressions a little bit easier
VBScript, Windows, IIS, ASP, Dev

Posted: 22 Jul 2001
Updated: 27 Aug 2001
Views: 83,931
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 4.21 Rating: 4.42 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 33.3%
4
2 votes, 66.7%
5

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Uwe Keim


Sitebuilder
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 is also teaching programming to students at the local "Fachhochschule".

In his free time, he does climbing, running and mountainbiking. You can watch him most of the day (and probably night) programming.

Some cool, free software from us:

Zeta Producer Desktop CMS
Intuitive, completely easy-to-use CMS for Windows.

Zeta Helpdesk
Open Source ticket- and helpdesk software for Windows and web.

Zeta Uploader
Easily send large files by e-mail. Windows and web client available.

Desargues
Innovative, lean and neat calculator for Windows. You never want to use Calc.exe again!
Occupation: Software Developer
Company: zeta software GmbH
Location: Germany Germany

Other popular ASP articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThis is a _slow_solutionmemberAnonymous4:49 31 Oct '01  
Generalexecuting a scriptmemberKonstantin S. Diguine23:55 24 Jul '01  
GeneralRe: executing a scriptmemberUwe Keim0:01 25 Jul '01  
GeneralregexmemberAnonymous21:58 23 Jul '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Aug 2001
Editor: Chris Maunder
Copyright 2001 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project