Comment On Regex, Schmegex

I'll admit it. When I first came across regular expressions, they seemed a bit intimidating. But, after spending about three or four minutes reading about them I felt a lot more comfortable. Apparently, Scott Elkin's colleague didn't feel like investing those three or four minutes learning regular expressions. Or looping through the individual characters on the string. Well ... at least this way it's all on one line. [expand full text]
« PrevPage 1Next »

re: Regex, Schmegex

2004-06-24 13:56 • by Scott C Reynolds
I'm speechless...wow.

re: Regex, Schmegex

2004-06-24 15:30 • by Thomas Eyde
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"

re: Regex, Schmegex

2004-06-24 15:38 • by Hassan Voyeau
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.

re: Regex, Schmegex

2004-06-24 16:04 • by Jason Hasner
nice.

re: Regex, Schmegex

2004-06-24 16:24 • by Alex Papadimoulis
Sorry, the language was VBScript in the Context of ASP ... CreateObject("Scripting.RegularExpression") --- or something like that gets you a regex.

re: Regex, Schmegex

2004-06-24 18:51 • by Stuck in my Ways
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.

re: Regex, Schmegex

2004-06-25 06:17 • by Bernhard Hofmann
I notice the lack of the correct regex for the less educated. Anyone fancy taking up the challenge?

re: Regex, Schmegex

2004-06-25 10:26 • by Tom Williams
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?

re: Regex, Schmegex

2004-06-25 11:02 • by Alex Papadimoulis
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).

re: Regex, Schmegex

2004-06-25 12:26 • by Scott C Reynolds
Tom...I know you didn't just come up in here and call that function fncGetRidOfNastyChars()...

re: Regex, Schmegex

2004-06-27 02:29 • by Jerry Pisk
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.

re: Regex, Schmegex

2004-07-14 22:39 • by The Wolf
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.

re: Regex, Schmegex

2004-09-09 11:51 • by Grrr
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.

Re: re: Regex, Schmegex

2007-12-29 11:04 • by - (unregistered)
168502 in reply to 22509
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?

Re: re: Regex, Schmegex

2007-12-30 17:49 • by psilo (unregistered)
168539 in reply to 22514
preg_replace('/[-!@#$%^&*)(_+=]/', '', $string);

fixed
« PrevPage 1Next »

Add Comment