• clambaker (unregistered)

    nice

  • (cs)

    In PHP :

    $text = preg_replace("/[!@#$%^&*()-_+=]/", '', $text);

    Hmm maybe the other way is simpler ?

  • Lenny (unregistered) in reply to clambaker
    Anonymous:
    nice


    Wow, insightful and humorus!  Thank you for sharing :)
  • OmnipotentEntity (unregistered) in reply to paranoidgeek

    yes, Perl:

    $text =~ s/[!@#$%^&*()-_+=]//;

  • (cs)

    While this is ugly, it's faster than looping through an array. So either he's dumb, or this code runs so often that it warrants a little optimization.  (Heh... optimization of VB/VBScript...)

  • (cs) in reply to OmnipotentEntity
    Anonymous:
    yes, Perl:

    $text =~ s/[!@\#\$%\^&*\(\)-_+=]//;

    Or...

    $text =~ s/\W//g;
  • OmnipotentEntity (unregistered) in reply to SerajewelKS

    umm... I wrote it to spec (I don't know if he wants to keep "s and :s and |s and such).  And that removes spaces too, though I admit to forgetting the global...  What you're looking for is:

    $text =~ s/[\W\S]//g;

    or perhaps

    $text =~ s/[?:\W\S]//g;

    if you have something in $1 that you want to keep.

  • OmnipotentEntity (unregistered) in reply to OmnipotentEntity

    Oh yeah, and he wants to get rid of s too, so

    $text =~ s/[?:\W\S
    ]//g;

  • (cs) in reply to OmnipotentEntity

    ... actually that just deletes every character in the string, or throw a syntax error because I'm confusing () with [] again.  Perhaps I should start thinking about this shit before I post. ¬_¬

    What I meant to say is:

    $text =~ s/[^a-zA-Z0-9\s]//g;

  • (cs) in reply to OmnipotentEntity

    But all this is moot, because AFAIK VB only supports regular expressions in a convoluted, complicated way.  And I promise to stop posting now.

  • (cs) in reply to SerajewelKS
    SerajewelKS:
    While this is ugly, it's faster than looping through an array. So either he's dumb, or this code runs so often that it warrants a little optimization.  (Heh... optimization of VB/VBScript...)


    It's /creatainly/ not faster.
    The replace is going to iterate the string per each symbol! Over & over again.
  • AC (unregistered) in reply to SerajewelKS

    what the fsck do you think the Replace() function does? VB compiles to native. 10+ function calls and 10+ variant allocs is faster than looping through an array? WTF? god explain that please

  • AC (unregistered) in reply to SerajewelKS
    SerajewelKS:
    While this is ugly, it's faster than looping through an array. So either he's dumb, or this code runs so often that it warrants a little optimization.  (Heh... optimization of VB/VBScript...)


    argh i meant to quote this weenie...

    Replace() will of course have to loop through something... argh
  • luis (unregistered) in reply to Ayende Rahien
    Ayende Rahien:
    It's /creatainly/ not faster. The replace is going to iterate the string per each symbol! Over & over again.

    I hope you don't mean to imply it's slower. Let n be the length of the string, and m the number of distinct symbols. Then in one case you're looping m times over the list of symbols, and in each iteration, you do an inner loop over the length of the string (n), where you check if the current char is the current symbol, and replace it if it is. In the other case, you loop over each character of the string, and then for each symbol, you check if the current char is that symbol. In both cases, it's n*m. The real inefficiency probably is that the Replace() function used needs to do a lot of bookkeeping, either by allocating a new string for the result, or by shifting characters towards the beginning of the string when you delete one. (The fast thing to do is to allocate a new, mutable string, iterate over the source string, and for each character that's not being removed, append it to the new string...)

  • (cs) in reply to luis

    Anonymous:
    (The fast thing to do is to allocate a new, mutable string, iterate over the source string, and for each character that's not being removed, append it to the new string...)

     

    And how is adding characters to a string in VB fast? It's almost the slowest operation possible [:P]. Maybe an array would do the job, 'join'ing it at the end. Anywho this person should be shot, because well.. do you know how many 'symbols' there are? He'd have been better off just copying letters to the target array or string.

    Drak

  • (cs) in reply to SerajewelKS
    SerajewelKS:
    While this is ugly, it's faster than looping through an array. So either he's dumb, or this code runs so often that it warrants a little optimization.  (Heh... optimization of VB/VBScript...)

    Well, that would be interesting test.  Internally VB is going to implement each one of those Replace calls with a loop (though probably implemented in nice, fast C/C++ code) through the array.  So all those nested Replace calls will eventually (as the level of nesting grows deeper and deeper) be slower than some code that can loop through the array just once.

  • (cs) in reply to Omnifarious

    It's burn in a way that is worse then table streching. I'm afraid to see the other half of the replacement query.

  • William (unregistered)

    nice...

    another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....

  • (cs) in reply to Omnifarious

    Omnifarious:
    Well, that would be interesting test.  Internally VB is going to implement each one of those Replace calls with a loop (though probably implemented in nice, fast C/C++ code) through the array.  So all those nested Replace calls will eventually (as the level of nesting grows deeper and deeper) be slower than some code that can loop through the array just once.
    I don't think the VB compiler will translate to C/C++ before it compiles it ...

    I've done regular expressions in VB6 and VBScript. 'Regex' is part of the "Scripting Runtime", and is used to interprete VBScript. It also has such interesting things as objects for files and folders (via the FileSystemObject). Most modern VB6 projects will have this DLL referenced anyway. So it is only a question of creating a object, and passing along the expression.

  • (cs)

    Are VB strings mutable or immutable?  If they're immutable, then this is allocating and GCing 14 strings every time it happens.  It's certainly a WTF, more like, "dude, learn how to use the library" kind of WTF.  It's certainly not an OMGWTF, like the false detector was.  Still, it's great to see the lengths people will go to to avoid having to use convenient built-in language features like regexps.  Scary to think what this programmer would do in Java.  "I will abstain from using 94% of java.* packages."

  • (cs)

    Without profiling, I would not be too sure this is slower than alternative implementations.
    Depending on how you build it (RE would of course be the best way, but not every programming language supports them) your own implementation may be slower.
    Let's say text="a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!" and lets assume the replace function is clever enough not allocate a new string if no replacement is actually made.
    One alternative implementation would loop through the string, check each character and remove it if it is a symbol: text=left(text, i-1)&mid(text,i+1)
    This creates 3 new string objects for each "!"
    Alternatively, you build a new string with all the characters which are not symbols:
    newtext=newtext&mid(text,i,1)
    This creates 2 new string objects for each "a"
    Chances are that both versions are slower than the original "WTF".
    Anyway, chances are you will not notice the difference in real life.
    At least you can say for sure that this implementation works. No doubt.
    The alternatives with loops require more testing, because they are more complex.

  • Masklinn (unregistered) in reply to William
    Anonymous:

    nice...

    another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....

    Problem's yours, I browse TDWTF just fine with Firefox 1.0.6

    Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.10) Gecko/20050716 Moonape/1.0.6

  • Bad Wolf (unregistered) in reply to William
    Anonymous:

    nice...

    another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....



    Hit refresh - worked for me.  Though the text then decided it was about 3 screens wide, so I had to do a lot of scrollbar work..
  • (cs) in reply to OmnipotentEntity
    OmnipotentEntity:
    ... actually that just deletes every character in the string, or throw a syntax error because I'm confusing () with [] again.  Perhaps I should start thinking about this shit before I post. ¬_¬

    What I meant to say is:

    $text =~ s/[^a-zA-Z0-9\s]//g;


    But that's mostly the same as \W, except that Perl takes the underscore as a "word character", so:
     $text =~ s/[\W_]//go;

        -dZ.

  • (cs) in reply to OmnipotentEntity
    OmnipotentEntity:
    But all this is moot, because AFAIK VB only supports regular expressions in a convoluted, complicated way.  And I promise to stop posting now.


    Actually, it supports it as an external Class, which is the way many other non-Perl languages do it.  As far as I know, Perl is one of the few (the only) programming languages that integrate Regular Expressions into their core syntax.  (Just like god intended RegExps to be.)

        -dZ.
  • (cs) in reply to DZ-Jay
    DZ-Jay:
    As far as I know, Perl is one of the few (the only) programming languages that integrate Regular Expressions into their core syntax.  (Just like god intended RegExps to be.)

        -dZ.


    I thought, correct me if I'm wrong, that Perl does not so much support regular expressions as much as it is a programming language that evolved from regular expressions. Which is arguably why so many C/ECMA/Java coders think a slab of Perl is alien-sp33k. It's like extensive regex with keywords and function names strewn in, instead of textual code with a regex here and there. :)
  • AndrewVos (argh im not logged in again) (unregistered) in reply to dhromed

    mmm, in Vb.Net it woulda looked soo much cooler :>

     = .Replace("%","").Replace("^") (repeat 256 times)

     

  • (cs)

    lol Just great. YMMD!

  • (cs) in reply to AndrewVos (argh im not logged in again)
    Anonymous:

    mmm, in Vb.Net it woulda looked soo much cooler :>

     = .Replace("%","").Replace("^") (repeat 256 times)



    Er, in Vb.net it wouldn't look too bad at all actually -

    strOut = Regex.Replace(strIn, "[^\w.@-]", "")

    (BTW ignore the RegEx I quote, but that's the syntax)

  • Richy (unregistered) in reply to paranoidgeek

    paranoidgeek:
    In PHP :

    $text = preg_replace("/[!@#$%^&\*\(\)\-_\+=]/", '', $text);

    Hmm maybe the other way is simpler ?

     

    $text = preg_replace('#[\W_]#', '', $text);

    Much nicer...

  • (cs) in reply to dhromed
    dhromed:

    I thought, correct me if I'm wrong, that Perl does not so much support regular expressions as much as it is a programming language that evolved from regular expressions.


    You're wrong.  Perl was designed from the very beginning as a programming language, with the built-in feature of manipulating text.  It borrowed heavily from text-manipulation tools (awk, for example) for it syntax and core functions, since those tools were very powerful at what they did, and their syntax and mode of operation was well known and understood.  Because of this, syntax for performing operations on Regular Expressions is built into the language itself, as opposed to having to call an external function or instantiate an object, like other programming languages require.  Of course, this will look alien to anybody not familiar with its syntax -- just like Japanese and Korean manuscripts look very alien to those who do not understand it (like me!).  This does not mean that it is inherently more complex or harder to learn than any other language -- after all, little Japanese kids read and write their language very naturally at an early age, just like Americans do with English.

        dZ.

  • Jason (unregistered)

    Some of these responses are a WTF. The regex /[!@#$%^&()_+=-]//g would suffice to do the replacement, assuming the "programmer" intended to ignore other puncuation. No need to backslash escape the special characters in the regex, just put the hyphen at the end. And it would run in O(n), not O(mn), because the regex uses a table lookup, not a comparison against each character. Though there would be some overhead from compiling the regex.

  • MORB (unregistered) in reply to Bad Wolf
    Anonymous:

    Hit refresh - worked for me.  Though the text then decided it was about 3 screens wide, so I had to do a lot of scrollbar work..


    That's because the code is all on one line, so it's actually a WTF from the original code.
  • (cs) in reply to DZ-Jay

    just like Americans do with English.

    Heh. You misspelt "American"

    Simon

    You're right about perl, though :D

  • (cs) in reply to MWTJ

    MWTJ:
    Are VB strings mutable or immutable?  If they're immutable, then this is allocating and GCing 14 strings every time it happens.  It's certainly a WTF, more like, "dude, learn how to use the library" kind of WTF.  It's certainly not an OMGWTF, like the false detector was.  Still, it's great to see the lengths people will go to to avoid having to use convenient built-in language features like regexps.  Scary to think what this programmer would do in Java.  "I will abstain from using 94% of java.* packages."

    VB Strings are immutable.  It will create a new string for every change to it.

  • (cs) in reply to SerajewelKS
    SerajewelKS:
    ... or this code runs so often that it warrants a little optimization.  (Heh... optimization of VB/VBScript...)
    Well if it is VB/VBScript then you need all the potimizing you can get!
  • Paul (unregistered) in reply to OmnipotentEntity
    OmnipotentEntity:
    ... actually that just deletes every character in the string, or throw a syntax error because I'm confusing () with [] again.  Perhaps I should start thinking about this shit before I post. ¬_¬

    What I meant to say is:

    $text =~ s/[^a-zA-Z0-9\s]//g;


    This is, arguably, the funniest fibre in this thread. Thank you for the laugh and the mini WTF.
  • (cs) in reply to William
    Anonymous:
    another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....
    The whole DailyWTF site is a WTF, we can't edit our posts and setting fonts attributes in the post editor is flakely.
  • phred (unregistered)

    Where I work we have to use that stuff, as regular expressions are forbidden, we aren't allowed to install any regular expression libraries as the GPL will corrupt our proprietory applications and open us up to lawsuits.

    But its not too bad as the code tends to be self documenting, unlike regular expressions which frequently have shell code embedded which can allow breakins to occur, this happens with perl too.

  • (cs) in reply to ammoQ

    ammoQ:
    Without profiling, I would not be too sure this is slower than alternative implementations.
    Depending on how you build it (RE would of course be the best way, but not every programming language supports them) your own implementation may be slower.
    Let's say text="a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!" and lets assume the replace function is clever enough not allocate a new string if no replacement is actually made.
    One alternative implementation would loop through the string, check each character and remove it if it is a symbol: text=left(text, i-1)&mid(text,i+1)
    This creates 3 new string objects for each "!"
    Alternatively, you build a new string with all the characters which are not symbols:
    newtext=newtext&mid(text,i,1)
    This creates 2 new string objects for each "a"
    Chances are that both versions are slower than the original "WTF".
    Anyway, chances are you will not notice the difference in real life.
    At least you can say for sure that this implementation works. No doubt.
    The alternatives with loops require more testing, because they are more complex.

    Why would one do any of these?  Is there no mutable String type in VB or something? Can you not create Strings from arrays of characters?

  • (cs) in reply to dubwai
    dubwai:

    ammoQ:
    Without profiling, I would not be too sure this is slower than alternative implementations.
    Depending on how you build it (RE would of course be the best way, but not every programming language supports them) your own implementation may be slower.
    Let's say text="a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!" and lets assume the replace function is clever enough not allocate a new string if no replacement is actually made.
    One alternative implementation would loop through the string, check each character and remove it if it is a symbol: text=left(text, i-1)&mid(text,i+1)
    This creates 3 new string objects for each "!"
    Alternatively, you build a new string with all the characters which are not symbols:
    newtext=newtext&mid(text,i,1)
    This creates 2 new string objects for each "a"
    Chances are that both versions are slower than the original "WTF".
    Anyway, chances are you will not notice the difference in real life.
    At least you can say for sure that this implementation works. No doubt.
    The alternatives with loops require more testing, because they are more complex.

    Why would one do any of these?  Is there no mutable String type in VB or something? Can you not create Strings from arrays of characters?

    toreplace = "!*/\&^%$#@"

    for i = 1 to len(toreplace)

      text = replace(text,mid$(toreplace,i,1),"")

    next i

    I'd do something like that, although it doesn't save on the string copying.

  • (cs)

    You know it's a WTF when you can only see half of it in your browser window.

  • Zathrus (unregistered) in reply to phred
    Anonymous:

    Where I work we have to use that stuff, as regular expressions are forbidden, we aren't allowed to install any regular expression libraries as the GPL will corrupt our proprietory applications and open us up to lawsuits.


    I'm sorry you work in such an utterly clue-free workplace. There are a number of non-GPL licensed RegEx libraries for most languages (those that don't just have them built-in that is).

    Anonymous:

    But its not too bad as the code tends to be self documenting, unlike regular expressions which frequently have shell code embedded which can allow breakins to occur, this happens with perl too.



    Oh, nevermind. You deserve to work there.
  • (cs) in reply to Zathrus
    Anonymous:
    Anonymous:

    Where I work we have to use that stuff, as regular expressions are forbidden, we aren't allowed to install any regular expression libraries as the GPL will corrupt our proprietory applications and open us up to lawsuits.


    I'm sorry you work in such an utterly clue-free workplace. There are a number of non-GPL licensed RegEx libraries for most languages (those that don't just have them built-in that is).

    Anonymous:

    But its not too bad as the code tends to be self documenting, unlike regular expressions which frequently have shell code embedded which can allow breakins to occur, this happens with perl too.



    Oh, nevermind. You deserve to work there.


    Just ignore the troll.



  • (cs) in reply to travisowens
    travisowens:
    Anonymous:
    another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....
    The whole DailyWTF site is a WTF, we can't edit our posts and setting fonts attributes in the post editor is flakely.


    IMO, not being able to edit your posts is no big loss;  it just forces you to proofread what you're posting.  I go to enough no-edit forums that I don't miss it.

    As to the 'whole site' being one big WTF, the only real issue is the javascript page browsing.  Everything else is sporadic enough that it qualifies as a PEBKAC error, not a problem with the site itself, unless you want to get into how the site displays in Opera or Safari.  That's just a standard MS WTF.
  • (cs) in reply to JThelen
    JThelen:
    As to the 'whole site' being one big WTF, the only real issue is the javascript page browsing.


    I agree.  Lose the form post in the paginator.  You can't refresh without choosing to repost the data.
  • (cs)

    I hate to burst everyone's bubble (actually I love to do that), but this is one of the best ways to do it in VB. I wrote a little test app that did the RegExp solution and it averaged 0.5 - 0.6 ms per execution. The replace version above ran in 0.1 ms. It may not be pretty, but it's much faster.

    I know some of you will complain that it's VBs fault that it wastes overhead and time instantiating the RegExp library and other "better" languages have it built-in. Who cares? We're talking about losing 0.4 ms of time per execution. So VBs strings are immutable. Just about any program written these days uses a few MB of memory at the least, you're talking about being wasteful because it uses a few more bytes to create new strings on function calls. At what point does it become wasteful to spend this much time devising a barely noticeable performance improvement?

  • (cs) in reply to Manni
    Manni:
    At what point does it become wasteful to spend this much time devising a barely noticeable performance improvement?

    At the point that you decided to use VB.

    Disclaimer: I'm not a VB-hater.  It's just no Porsche.
  • (cs) in reply to Mung Kee

    Mung Kee:
    Manni:
    At what point does it become wasteful to spend this much time devising a barely noticeable performance improvement?

    At the point that you decided to use VB.

    Disclaimer: I'm not a VB-hater.  It's just no Porsche.

    If my shitty car gets up to 60 mph in the same time a Porsche can get to 130 mph, I call that a significant difference. If you think you can save a few hundreths of a millisecond by using one language as opposed to another, then I'm not convinced your language is better.

    And by the way, I was looking for a real answer, not a smart-assed comment that provided no further insight about when the coding time investment does not yield appropriate performance returns.

    "Ha ha VB sux"...yeah, thanks for contributing.

  • Humble merc coder (unregistered) in reply to OmnipotentEntity
    umm... I wrote it to spec

    ...And thus begins many a sad tale.

Leave a comment on “Just in case SPACE isn't supported in the future”

Log In or post as a guest

Replying to comment #40308:

« Return to Article