| Related sites for http://www.aspalliance.com/brettb/VBScriptRegularExpressions.asp |
| Java_Software_Human_Interface Essential information about human interface design and usability for applications and applets written in the Java programming language. | | Dingbat_Depot Archive of free dingbat fonts. Also contains a dingbat message board, most popular dingbats and links section. | | Digital_Web_Book_Reader A freeware reader for digital eBooks produced in the proprietary DNL format. | | ASPG___Advanced_Software_Products_Group Software company that develops and distributes z/OS and VSE/ESA systems software used for Storage Administration, Capacity Planning, Data Security, Disaster Recovery, System Productivity and CICS Prod | | FindFile_ActiveX_Component An ActiveX component that facilitates searching a directory or a set of subdirectories for the oldest or most recent file based on its creation or modification date. | | Accomasystems Independent AS/400 contract consultant with experience in system design, conversions and migrations. | | Jar_Media Offers design services including accessible website design and development, hosting, interactive TV design, video editing, Flash design, DVD interface design, motion graphics, corporate identity, and | | IBM_Advanced_Computing_Systems A description of the first superscalar computer system - the IBM ACS project in the 1960s. | | EaseSoft_Barcode .Net Windows Forms control and ASP.NET control for creating barcode images. | | ASG_Software_Solutions Provides highly integrated software products and specialized consulting services designed to manage and automate the evolution of large-scale existing COBOL applications software systems. | | Haskell_Mode_for_Emacs An Emacs mode currently supporting font locking, declaration scanning, documentation, indentation, hugs interaction. | | Build Open Source - A software build system implemented on top of GNU make featuring position-independent non-recursive multi-makefile include-based structure, complete inter-project dependency tracking, an | | North_American_Network_Operators\'_Group Informal association of North American internet backbone operators. Hosted by MERIT. | | Robot_Systems_Services A programming and training service dedicated to Fanuc Robotics. Located in Belgium. Site does not work in some browsers. | | ACSW05 Australasian Computer Science Week. Newcastle, Australia; 30 January -- 3 February 2005. | | Java,_Ruby,_and_Sun Brief story of Sun hiring Charles Nutter and Thomas Enebo to work on JRuby full time, with speculations. Ablog. (September 8, 2006) | | Mozilla_Firefox_Blog A polish weblog about miscellaneous Firefox topics. | | SiteOne Offers design, development, NT hosting, and domain registration services. Provides development using Active Server Pages, JavaScript, DHTML, XML, and Flash. | | eStudio_Media Specialists in definition, design, deployment of static and interactive commercial web sites and applications. | | Planet_Lex_Software Several screen savers, with color and shape variations. Demo versions available. |
|
VBScript Regular Expressions
function SubmitSearch() {
document.frmSearch.submit();
}
ASP Kitchen Search: Go
Home | ASP Articles | ASP.NET Articles |
SQL Server Articles |
Code Documentation Tools | Table Of Contents |
ASP Kitchen: ASPWatch.com
articles: VBScript Regular Expressions
VBScript Regular Expressions
Introduction
From a programming perspective, one of the biggest annoyances I have faced with
VBScript is the lack of decent string handling functions. Sure, with a bit of imagination,
functions such as InStr, Left and Mid can be combined to help with the job at hand. But
for anyone used to the Perl programming language, VBScript can appear to be extremely
inflexible.
VBScript Regular Expressions
While still being someway off the effectiveness of Perl, version 5 of VBScript now has
much improved text handling functions, through it's support for Regular Expressions. For
anyone who hasnt encountered the term, Regular Expressions have been an essential
part of that nasty operating system known as Unix for many years (and where considered so
useful that they were incorporated into Perl). They can be cryptic and difficult to learn,
but they allow sophisticated pattern matching in strings.
Regular Expressions are quite involved (it is possible to buy whole books devoted to
their use!), and the purpose of this article is simply to draw your attention to the
potential of using them within VBScript.
If you want to know more, then try some of the references at the bottom of the article.
Using the VBScript RegEx object
Support for Regular Expressions has been included in VBScript by the inclusion of the
RegExp object in VBScript version 5. This is the version of the VBScript scripting engine
released with Internet Explorer 5. If you dont have IE5 on your server, but want to
upgrade the scripting engine, then download the new scripting engine from www.microsoft.com/scripting.
If you dont know which version of the scripting engine you are using, then try
using the following small script:
<%
Response.Write "You are using "
Response.Write ScriptEngine
Response.Write " version " & ScriptEngineMajorVersion & "."
& ScriptEngineMinorVersion
%>
This should display a message such as the one below:
Using the RegExp object
Once you have determined that your web server is running the correct version of
VBScript, the following lines of code will demonstrate the use of the RegExp object.
The first line of code will create a new string that will be searched for the existence
of a sub-string:
StringToSearch = "http://www.foo.com"
The RegExp object can then be created:
Set RegularExpressionObject = New RegExp
This object has three properties: Pattern, IgnoreCase and Global. Pattern specifies the
Regular Expression that should be searched for. IgnoreCase should be True or False
depending on whether the search should be case sensitive (the default is true). Finally,
the Global property should be set to True if the search should match all occurrences of
the pattern, or False if just the first occurrence should be matched:
With RegularExpressionObject
.Pattern = ".com"
.IgnoreCase = True
.Global = True
End With
Once the RegExp objects properties have been set, it is time to test the Regular
Expression. This is done using something like the line below. This uses the Test method of
the RegExp object to see if the Regular Expression is found in the StringToSearch string.
expressionmatch = RegularExpressionObject.Test(StringToSearch)
The Test method will return True if the Regular Expression was found, and False if it
was not found.
If expressionmatch Then
Response.Write RegularExpressionObject.Pattern & " was found in " &
StringToSearch
Else
Response.Write RegularExpressionObject.Pattern & " was not found in " &
StringToSearch
End If
Finally, the RegExp object is destroyed since it is no longer required.
Set RegularExpressionObject = nothing
More RegExp object methods
As well as the Test method, the VBScript RegExp object has two further methods: Execute
and Replace.
The RegExp Execute method
The RegExp Execute method is a more sophisticated version of the Test method. As well
as seeing if the Regular Expression is found within a string, it will also return the
number of matches made within that string, and at which position in the string the
match(es) were made. An example of its use is below:
<%
StringToSearch = "The website ASPWatch.com contains lots of information about ASP,
asp, as well as Asp."
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "ASP"
.IgnoreCase = False
.Global = True
End With
Set expressionmatch = RegularExpressionObject.Execute(StringToSearch)
If expressionmatch.Count > 0 Then
For Each expressionmatched in expressionmatch
Response.Write "<B>" & expressionmatched.Value & "</B>
was matched at position <B>" & expressionmatched.FirstIndex &
"</B><BR>"
Next
Else
Response.Write "<B>" & RegularExpressionObject.Pattern &
"</B> was not found in the string: <B>" & StringToSearch &
"</B>."
End If
Set RegularExpressionObject = nothing
%>
As with the Test method, the RegExps Global and IgnoreCase properties are useful.
The RegExp Replace method
This can be used to replace a part of a string using Regular Expression matching. For
example, in the script below, .co.uk is changed into .com:
<%
InitialString = "www.foo.co.uk"
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = ".co.uk"
.IgnoreCase = True
.Global = True
End With
ReplacedString = RegularExpressionObject.Replace(InitialString, ".com")
Response.Write "Replaced " & InitialString & " with " &
ReplacedString
Set RegularExpressionObject = nothing
%>
Real life Regular Expressions
So far, this article has shown how to use the VBScript RegExp object to manipulate and
test strings, but there is nothing here that couldnt already be done with other
VBScript functions. The power of Regular Expressions only become apparent when more
complex situations are encountered. For example, the VBScript function below will strip
out all the HTML tags from strings:
<%
Function stripHTMLtags(HTMLstring)
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "<[^>]+>"
.IgnoreCase = True
.Global = True
End With
stripHTMLtags = RegularExpressionObject.Replace(HTMLstring, "")
Set RegularExpressionObject = nothing
End Function
%>
The function can then be called using something like:
<%
Response.Write stripHTMLtags("<B>This <I>is</I> <TT
style=""background-color: rgb(0,255,255)"">some</TT> <FONT
COLOR=#FF00FF> HTML</FONT></B>")
%>
The function works because it replaces HTML tags with a null character. HTML tags are
identified using the Regular Expression held in the Pattern property. This is a sequence
of special characters. This means that a HTML tag should start with a "<". It
should then contain one or more characters except for a greater than sign
">". This is indicated by enclosing the greater than sign in square brackets,
and using the plus sign (which means match the preceding character one or more times. The
^ symbol denotes that the character should NOT appear. Finally, it should contain a
greater than sign to close the HTML tag.
Looks complicated? Unfortunately most things that originate on Unix systems are! The best
way to figure out Regular Expressions is to play around with them check the bottom
of this article for guides to the various characters that can be used. As a further help,
a couple more regular expressions are shown below:
The dollar sign is used to look for matches at the end of a string, so the following will
look for .co.uk at the end of a string:
.Pattern = ".co.uk$"
Use a bar to specify that several expressions should be matched. The following will
match .co.uk or .com at the end of a string:
.Pattern = ".co.uk$|.com$"
Using Regular Expressions in Visual Basic
Few people seem to know that Regular Expressions can also be used with Visual Basic 5
or 6. All you need to do is to include a reference to "Microsoft VBScript Regular
Expressions 1.0" in your project.
The following Visual Basic sample code will replace some HTML in a TextBox called Text1
with plain text:
Dim RegularExpressionObject As New VBScript_RegExp_10.RegExp
With RegularExpressionObject
.Pattern = "<[^>]+>"
.IgnoreCase = True
.Global = True
End With
Text1.Text = RegularExpressionObject.Replace(Text1.Text, "")
Set RegularExpressionObject = Nothing
Note that more recent versions of the VBScript Regular Expressions library now exist,
so it may also be possible to make a reference to "Microsoft VBScript Regular
Expressions 5.5". If this library is used then the first line of the Visual Basic
code will have to be modified to:
Dim RegularExpressionObject As New VBScript_RegExp_55.RegExp
Further Reading
Regular Expressions are available in recent versions of JavaScript, so Netscape have
provided a guide to
their use.
Microsoft
Beefs up VBScript with Regular Expressions - Microsoft's guide to using Regular
Expressions with VBScript.
A list of
special characters that can be used in Regular Expressions is available in the
VBScript 5 Language Reference. (at msdn.microsoft.com/scripting).
Regular Expression Library - A small but growing
library of Regular Expressions suitable for use with VBScript. Users can add new regular
expressions, and rate those that are already there.
These two books are invaluable if you want to make extensive use of Regular Expressions
with any language that supports them:
Useful Development Tools
ASP
Documentation Tool
Automatically creates technical documentation for ASP 2.0
and 3.0 web applications written in VBScript and JScript. Documentation for Microsoft
Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the
web application can also be incorporated into the reports. Documentation is created in
HTML, HTML Help and plain text formats.
View Sample
Output (HTML Help format).
View Sample Output (HTML Format).
Download
Trial Version (5.2Mb ZIP file).
.NET Documentation Tool
Automatically creates technical documentation for .NET Framework applications written in C# or VB.NET (including ASP.NET).
Documentation for SQL Server 7/2000/2005 databases and C#/VB.NET components associated with the
web application can also be incorporated into the reports. Documentation is created in
HTML, HTML Help and plain text formats. Additional support for ASP.NET web applications. A useful alternative to the popular NDoc code documentor
View Sample
Output (HTML Help format).
View Sample Output (HTML Format).
Download
Trial Version (3Mb ZIP file).
SQL
Documentation Tool
The SQL Documentation Tool creates technical documentation for Microsoft SQL Server 7.0, 2000 and 2005 databases. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced. The SQL Documentation Tool documents SQL Server Tables, Views, Stored Procedures, Triggers, Table Relationships, Jobs and DTS Packages.
View Sample
Output (HTML Help format).
View Sample Output (HTML Format).
Download
Trial Version (10.3Mb ZIP file).
VB
Documentation Tool
The VB Documentation Tool creates technical documentation for Microsoft Visual Basic 6.0 projects. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced.
View Sample
Output (HTML Help format).
View Sample Output (HTML Format).
Download
Trial Version (1Mb ZIP file).
The Website Utility
The Website Utility examines websites for errors and
areas that need to be optimised for search engines by using a built in web crawling engine.
Errors checked for include broken or moved hyperlinks, missing page titles and missing meta tags.
It also generates HTML for use in creating website site maps (table of contents pages - like this one), and is
able to create both client-side JavaScript search engines and server-side ASP search engines and ASP.NET search engines for a website.
View Sample Output (HTML Format).
Download
Trial Version (3Mb ZIP file).
Text Workbench
Text Workbench is a file search and replacement utility for text files and Microsoft Office documents. Make rapid file replacements on multiple files and folders full of files. Advanced replacement options include regular expressions support. It even works on remote file systems via FTP. A Regular Expression Laboratory allows advanced pattern matching and replacement expressions to be built and tested. This great utility will make your everyday development tasks much easier!
Download
Trial Version (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation).
Indexing Service Companion
The Indexing Service Companion is a utility that extends the functionality of the Microsoft Windows Indexing Service so that it is able to index content from any remote website and also from ODBC compliant databases. As such it can be used as a low cost alternative to Sharepoint's Search Services.
View Product
Documentation (119K ZIP file).
Try Sample Search Facility.
Download
Trial Version (1.7Mb ZIP file).
ASP Spell Check
ASPSpellCheck is the easy way to add spell checking capabilities to your ASP or ASP.NET websites, Intranets and web applications. The utility allows you to add spell checking capabilities to any HTML text field or rich content editing text box. It works with all common web browsers, and there are no components or databases to install on the server.
Read ASPSpellCheck Review.
View ASPSpellCheck Examples.
Download
Trial Version (3Mb ZIP file; you have the option to either install directly from this link or save the file for later installation).
Author details
Brett Burridge
has worked as a web developer since 1997 and has developed web applications for a range of corporations, start up busiensses and educational establishments.
Brett is presently employed as an Internet developer and technical writer
through his own company,
Winnersh Triangle Web Solutions Limited.
The company produces a number of
innovative products, including a range of software documentation tools, which include the
ASP Documentation Tool, the .NET Documentation Tool for VB.NET and C#, and the SQL Server Documentation Tool. Other products include The Website Utility, which functions as a website error checker, search engine optimizer and ASP/ASP.NET search engine builder application.
As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk,
ASPToday, the software documentation portal www.softwaredocumentation.info, and has contributed recipes to the ASP.NET Developer's Cookbook.
links
Outside web development, Brett is interested in travelling (here are my travel logs from New York, Hong Kong and Tokyo), digital photography (here's my photo gallery), tropical fishkeeping
and collecting contemporary works of art by artists such as Doug Hyde.
Contact Brett by emailing |
|