|
-
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.
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
|