Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 12th, 2009, 01:48 AM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
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
Reply With Quote
  #2 (permalink)  
Old March 12th, 2009, 10:19 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
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
Reply With Quote
The Following User Says Thank You to philip_cole For This Useful Post:
mat41 (March 12th, 2009)
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
REG EXPRESSION pallone Javascript How-To 4 August 28th, 2008 10:37 AM
Reg:ShowModalDialogBox arivarasuk C# 1 August 12th, 2008 07: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



All times are GMT -4. The time now is 10:17 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.