• Scott C Reynolds (unregistered)

    I'm speechless...wow.

  • Thomas Eyde (unregistered)

    People like me, who doesn't grok regex would do it a little harder:

    Const invalidCharacters = "!@#$%^&*()-_+="
    Const sampleText = "Sample " + invalidCharacters + "text"


    Dim text As String
    text = sampleText


    Dim i As Long
    For i = 1 To Len(invalidCharacters)
    Dim match As String
    match = Mid(invalidCharacters, i, 1)


    text = Replace(text, match, "")
    Next i


    Debug.Assert text = "Sample text"

  • Hassan Voyeau (unregistered)

    What language is this anyway. Maybe the language used here does not have a find/replace using reg expressions, or even loops. Rule #1, never make any assumptions.

  • Jason Hasner (unregistered)

    nice.

  • Alex Papadimoulis (unregistered)

    Sorry, the language was VBScript in the Context of ASP ... CreateObject("Scripting.RegularExpression") --- or something like that gets you a regex.

  • Stuck in my Ways (unregistered)

    I'd probably do something extremely inefficient like:

    dim newString
    for i=1 to text.length
    select case asc(mid(text,i,1))
    case 48 to 57 'Numbers
    newString &= mid(text,i,1)
    case 65 to 90 'Uppercase
    newString &= mid(text,i,1)
    case 97 to 122 'Lowercase
    newString &= mid(text,i,1)
    case else 'Invalid character
    'Do nothing here :)
    next i

    or maybe slightly more efficiently:
    For i = 32 to 47:replace(text,char(i),""):next i
    For i = 58 to 64:replace(text,char(i),""):next i
    For i = 91 to 96:replace(text,char(i),""):next i
    For i = 123 to 127:replace(text,char(i),""):next i

    ok, ok, maybe that's not any more efficient...
    Gotta look into these "regular expressions" :) They sound pretty cool.

  • Bernhard Hofmann (unregistered)

    I notice the lack of the correct regex for the less educated. Anyone fancy taking up the challenge?

  • Tom Williams (unregistered)

    How about:
    Function fncGetRidOfNastyChars(ByVal Text As String) As String

    Dim Exp as New Regex("[!@#$%^&*()-_+=]+")

    Text = Exp.Replace(Text, "")

    fncGetRidOfNastyChars = Text

    End Function

    That might work....

    They might need to be escaped using the \ - I'm not sure in this instance, as you are supplying a specific list of characters...

    Anyone got any better ideas?

  • Alex Papadimoulis (unregistered)

    Oh What the heck ... aparantly no one reads documentation ... or even Quickstart tutorials :-D (http://samples.gotdotnet.com/quickstart/howto/doc/regexreplace.aspx)

    how about keeping the chars we want ...

    myRegex = New Regex("[^A-Za-z0-9 ]")
    myRegex.Replace( theText, "")

    ... replaces all characters except (the ^ means not in) except uppercase letters (A-Z), lower case letters (a-z), numbers (0-9), and spaces. But its just as easy to go either way (define chars you want vs define chars you dont want).

  • Scott C Reynolds (unregistered)

    Tom...I know you didn't just come up in here and call that function fncGetRidOfNastyChars()...

  • Jerry Pisk (unregistered)

    Alex, it's actually a lot safer to go the inclusive way, just say what's a valid value and don't think about all the possible invalid ones. Chances are you'll miss one. So if all you accept are US ASCII letters and digits then you should use the regex you supplied.

  • The Wolf (unregistered)

    PHP Developers know the best way to do this without regex...

    str_replace(array('!', '@', '#'), '', $string);

    I don't know if its faster than using a regex but its simpler to read.

  • Grrr (unregistered)

    That would be "simpler" only if you have a couple of chars.
    Plus, PHP developers can do it just as well with the regexp:

    preg_replace("/!@#$%^&*()-_+=/", "", $string);

    beats using array(), at least for me.

  • - (unregistered) in reply to Tom Williams
    Tom Williams:
    How about: Function fncGetRidOfNastyChars(ByVal Text As String) As String

    ...

    Anyone got any better ideas?

    Yes, why bother with "fnc" in the function name? Isn't it obvious that it's a function?

  • psilo (unregistered) in reply to Grrr

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

    fixed

Leave a comment on “Regex, Schmegex”

Log In or post as a guest

Replying to comment #22511:

« Return to Article