Click here to Skip to main content
6,292,426 members and growing! (10,188 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, ASP, IIS, Dev
Posted:22 Jul 2001
Updated:27 Aug 2001
Views:91,544
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
9 votes for this article.
Popularity: 4.21 Rating: 4.42 out of 5

1

2

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


Member
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 teached programming to students at the local university.

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

Some cool, free software from us:

-----

Zeta Test

Zeta Test is an integrated test management environment that enables you to perform black-box tests, white-box tests, regression tests or change management tests of software applications.

Zeta Test helps you to plan, perform, log, monitor and document the tests, and then to evaluate the test results.

Create and manage your test cases and test plans with Zeta Test. Test your software with test scripts that you created with Zeta Test.

Directly download Zeta Test for free!

-----

Zeta Producer Desktop CMS

Intuitive, completely easy-to-use CMS for Windows. Both Freeware version and full version available.

Try out by yourself now! (direct download)

-----

Zeta Uploader

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

-----
Occupation: Software Developer
Company: zeta software GmbH
Location: Germany Germany

Other popular ASP articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralThis is a _slow_solution PinmemberAnonymous4:49 31 Oct '01  
Generalexecuting a script PinmemberKonstantin S. Diguine23:55 24 Jul '01  
GeneralRe: executing a script PinmemberUwe Keim0:01 25 Jul '01  
Generalregex PinmemberAnonymous21: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-2009
Web09 | Advertise on the Code Project