Results 1 to 8 of 8

Thread: SmartReplace challenge

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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:
    1. Public Function SmartReplace(Expression As String, Find As String, Replace As String, _
    2. Optional Delimiters As String = " ", Optional ByVal Start As Long = 1, Optional ByVal Count As Long = -1, _
    3. Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As String
    4.     ' code here
    5. 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.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Private Function SmartReplace(sExpression As String, sFind As String, sReplace As String, _
    2.                               Optional sDelimiters As String = " ", Optional ByVal Start As Long = 1, _
    3.                               Optional ByVal Count As Long = -1) As String
    4.                              
    5.     Dim sParts() As String, sPart As String, sMatch As String
    6.     Dim N As Long, lStart As Long, lEnd As Long
    7.    
    8.     sParts = Split(Replace(sFind, "*", vbNullChar), vbNullChar)
    9.     For N = 0 To UBound(sParts)
    10.         If Len(sParts(N)) > Len(sPart) Then sPart = sParts(N)
    11.     Next N
    12.    
    13.     SmartReplace = sExpression
    14.     N = Start
    15.    
    16.     Do
    17.         N = InStr(N, SmartReplace, sPart)
    18.         If N Then
    19.             lStart = InStrRev(SmartReplace, sDelimiters, N) + 1
    20.             lEnd = InStr(N + Len(sPart), SmartReplace, sDelimiters)
    21.             If lEnd = 0 Then lEnd = Len(SmartReplace)
    22.             If Mid$(SmartReplace, lStart, lEnd - lStart) Like sFind Then
    23.                 SmartReplace = Left$(SmartReplace, lStart - 1) & sReplace & Mid$(SmartReplace, lEnd)
    24.                 N = N + Len(sReplace)
    25.                 Count = Count - 1
    26.             Else
    27.                 N = N + 1
    28.             End If
    29.         End If
    30.     Loop While N > 0 And Count > 0
    31. 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!

  3. #3
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    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

  4. #4
    New Member
    Join Date
    Jan 2008
    Posts
    1

    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.

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

  6. #6

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: SmartReplace challenge

    Just giving a heads up to the new guy that posted two days ago.

  8. #8
    New Member
    Join Date
    Feb 2009
    Posts
    1

    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

    Quote 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
  •  



Click Here to Expand Forum to Full Width