Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 12th, 2009, 12:48 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default Reg Ex help

This function:

function strCount(theStr, subStr)
strCount = UBound(split(theStr, subStr))
end function

simply counts the occurance of one string within another. In this situation I have a string of EG:

myStr = "YES,NO,NO,NO,YES"

So I am passing strCount(myStr, "YES") However its possible the string could get longish therefore thought reqular expressions would be the most efficient way to get my result. I am Cr*p at them, anyone care to help? Yes I know I should have a bit more of an interest in expressions and learn them better

TYIA
__________________
Wind is your friend
Matt
 
Old March 12th, 2009, 09:19 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Matt,

Here is a simple way to do it, just setting the pattern as the match string.

vb Code:
Function strCount(theStr, subStr)

Set re = New RegExp
re.Pattern = subStr
re.Global = True

Set oMatches = re.Execute(theStr)
strCount = oMatches.Count

End Function

The Execute method will create a collection of matched strings, and the Count property tells you how many were found.
You have to be careful with using regular expressions, as if you are looking for substrings with non-letter characters in, such as * or (), they would be used as special pattern matching constructs within the regexp, and so you would probably get the wrong results back. You could get possibly get round this by escaping these characters by adding \ before them, but that obviously requires another regexp and more work.

On another note, the simple text matching used in the Split function is generally quicker than that used by regular expressions, as it is just matching characters rather than patterns. You could build a hybrid function which uses the Split method if theStr is less than, say, 50 characters, and regular expressions if longer.

HTH
Phil
The Following User Says Thank You to philip_cole For This Useful Post:
mat41 (March 12th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
REG EXPRESSION pallone Javascript How-To 4 August 28th, 2008 09:37 AM
Reg:ShowModalDialogBox arivarasuk C# 1 August 12th, 2008 06:38 AM
reg ex help swarnap Javascript How-To 1 January 9th, 2007 02:14 AM
reg ex help swarnap Javascript 1 January 8th, 2007 08:24 AM
Reg Sum() function suri_1811 XSLT 2 December 7th, 2006 03:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.