|
-
Jan 15th, 2007, 05:48 AM
#1
SmartReplace challenge
On our second series of challenges we have a smart replace function (an idea by jcis). The syntax is nearly identical to the regular Replace function, but this time we allow for some wildcards to be used. There is also some special behavior involved. Here is a sample procedure:
VB Code:
Public Function SmartReplace(Expression As String, Find As String, Replace As String, _
Optional Delimiters As String = " ", Optional ByVal Start As Long = 1, Optional ByVal Count As Long = -1, _
Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As String
' code here
End Function
First and foremost, the wildcards are blocked by space characters and thus replace never occurs on them. Other blocking characters can be customized through the Delimiters parameter, although this parameter is optional and is not required in this challenge. Delimiters are not replaceable and they must never be removed unless they are contained in the Find parameter.
The wildcards match the rest of the words between the delimiting characters, and if a match is found, a replace is processed.
Wildcards and special characters- * = none, or any number of characters
- ? = any one character (not a requirement)
- # = any one number (not a requirement)
- \ = escape character (not a requirement)
Escape character allows matching for the wildcard characters, ie. \* does represent the character *, and it is not the wildcard *. \\ would then be a single \ character.
Sample calls
Expression = "abc abcd abcde AAA"
Find = "abc*"
Replace = "1"
Result: "1 1 1 AAA"
Expression = " abc cabc abzt"
Find = " ab*"
Replace = "P"
Result: "P cabcP".
Expression = "Hi, how are you?"
Find = "y*u"
Replace = "they"
Result: "Hi, how are they?"
Expression = "123 - 267 = 0"
Find = "*2*"
Replace = "5"
Result: "5 - 5 = 0"
Your function may optionally support these
Expression = "123 + 2345 + 345 + 45"
Find = "###"
Replace = "666"
Result: "666 + 2345 + 666 + 45"
Expression = "A12 + B34 + CDE"
Find = "?##"
Replace = "I'm happy"
Result: "I'm happy + I'm happy + CDE"
Expression = "One * Two = Three"
Find = "\*"
Replace = "+"
Result: "One + Two = Three"
About other parameters
Delimiters = characters delimiting replaceable items from each other.
Start = starting position: characters before this one are ignored.
Count = number of replaces: job is done once this amount of replaces are done or we have come to the end of string.
Compare = compare method: binary mode or text mode.
-
Jan 18th, 2007, 08:28 PM
#2
Re: SmartReplace challenge
well, no one else has posted so I figured I'd throw something (short) together.
It supports *, Delimiters, Start and Count. It won't however allow you to include the delimiter in the Find string (i disagree with how that should be interpreted anyway) - i.e. it'll work with 1, 3 & 4 above
VB Code:
Private Function SmartReplace(sExpression As String, sFind As String, sReplace As String, _
Optional sDelimiters As String = " ", Optional ByVal Start As Long = 1, _
Optional ByVal Count As Long = -1) As String
Dim sParts() As String, sPart As String, sMatch As String
Dim N As Long, lStart As Long, lEnd As Long
sParts = Split(Replace(sFind, "*", vbNullChar), vbNullChar)
For N = 0 To UBound(sParts)
If Len(sParts(N)) > Len(sPart) Then sPart = sParts(N)
Next N
SmartReplace = sExpression
N = Start
Do
N = InStr(N, SmartReplace, sPart)
If N Then
lStart = InStrRev(SmartReplace, sDelimiters, N) + 1
lEnd = InStr(N + Len(sPart), SmartReplace, sDelimiters)
If lEnd = 0 Then lEnd = Len(SmartReplace)
If Mid$(SmartReplace, lStart, lEnd - lStart) Like sFind Then
SmartReplace = Left$(SmartReplace, lStart - 1) & sReplace & Mid$(SmartReplace, lEnd)
N = N + Len(sReplace)
Count = Count - 1
Else
N = N + 1
End If
End If
Loop While N > 0 And Count > 0
End Function
regarding comments made in the QuoteSplit thread about RegExp - there's still enough need for something like this is if you only want a simple SmartReplace, or if you perhaps don't want another dependency.
Anyway, it looks as though your attempts to revive the contests are dying a death here, Merri!
-
Jan 24th, 2007, 04:18 AM
#3
Addicted Member
Re: SmartReplace challenge
I'm coming im coming - exams over .
Coursework started .
Well Ill try to get something done soon.
Honestly though, I'm moving to other programming languages... I've hacked VB too much. However, my Speed4VB (New name :P), will be completed sometime - replaces VB native functions with FAST x86 assembly equivalents.
Cheers
-
Jan 22nd, 2008, 09:51 AM
#4
New Member
Re: SmartReplace challenge
I was looking for something just like what you posted. Thanks. It works great.
So far the only change I made was to change the count parameter to be a boolean flag for replace 1st or replace all.
-
Jan 23rd, 2008, 02:47 PM
#5
Re: SmartReplace challenge
This is exactly why microsoft built the regular expression classes. Take the energy you're using to create your own parsing language, and learn one that's already been developed.
-
Jan 24th, 2008, 09:02 AM
#6
Re: SmartReplace challenge
You're writing in a wrong thread in a sense that I didn't originally think this as a great idea, either; however, people seemed to want to have a shot at this, so I put up the challenge. As you can see, it didn't quite catch up, so giving it some critic is kind of pointless. And the thread is over a year old, too.
As for regular expressions, despite them being useful sometimes, personally I find them complex to manage in a code: you really really need to study them long and hard to get good enough to be able to read them correctly right on. Thus I think regular expressions have a big negative effect on code readability. Thus my opinion: they're useful, but each time they're used the line should be heavily commented.
-
Jan 24th, 2008, 11:04 AM
#7
Re: SmartReplace challenge
Just giving a heads up to the new guy that posted two days ago.
-
Feb 6th, 2009, 05:15 AM
#8
New Member
Re: SmartReplace challenge
Hi Readwulf,
Did you manage to get your new fast x86 functions written? I've seen you've posted on the ThunderVB project with an Instr replacement some time ago. But I've found a bug in it and have also found this post. Just wondering if you've got a simpler replacement I can use within VB6.
Jon
 Originally Posted by Raedwulf
I'm coming im coming - exams over  .
Coursework started  .
Well Ill try to get something done soon.
Honestly though, I'm moving to other programming languages... I've hacked VB too much. However, my Speed4VB (New name :P), will be completed sometime - replaces VB native functions with FAST x86 assembly equivalents.
Cheers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|