- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I'm speechless...wow.
Admin
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"
Admin
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.
Admin
nice.
Admin
Sorry, the language was VBScript in the Context of ASP ... CreateObject("Scripting.RegularExpression") --- or something like that gets you a regex.
Admin
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.
Admin
I notice the lack of the correct regex for the less educated. Anyone fancy taking up the challenge?
Admin
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?
Admin
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).
Admin
Tom...I know you didn't just come up in here and call that function fncGetRidOfNastyChars()...
Admin
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.
Admin
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.
Admin
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.
Admin
Yes, why bother with "fnc" in the function name? Isn't it obvious that it's a function?
Admin
preg_replace('/[-!@#$%^&*)(_+=]/', '', $string);
fixed