| « Prev | Page 1 | Next » |
|
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" |
|
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.
|
|
Sorry, the language was VBScript in the Context of ASP ... CreateObject("Scripting.RegularExpression") --- or something like that gets you a regex.
|
|
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. |
|
I notice the lack of the correct regex for the less educated. Anyone fancy taking up the challenge?
|
|
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? |
|
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). |
|
Tom...I know you didn't just come up in here and call that function fncGetRidOfNastyChars()...
|
|
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.
|
|
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. |
|
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. |
Yes, why bother with "fnc" in the function name? Isn't it obvious that it's a function? |
Re: re: Regex, Schmegex
2007-12-30 17:49
•
by
psilo
(unregistered)
|
|
preg_replace('/[-!@#$%^&*)(_+=]/', '', $string);
fixed |
| « Prev | Page 1 | Next » |