Question Simple System.Text.RegularExpressions Request

Untamed

Well-known member
Joined
Jul 25, 2009
Messages
53
Programming Experience
5-10
Hello all, after searching for around a half hour or so, I found that the regularexpression API could do what I need.
Basically, I needed to only allow letters, numbers, and symbols in my textbox. The problem was, I could stick alt codes in there, even with a keycode case check.

So, I found regularexpressions, and it appears to do what I need.
The problem is, I only have an expression working for letters and numbers, and I need symbols in it too.

Here is the one I tried to use that doesn't really work...
If Not Regex.IsMatch(TextBox1.Text, "[^A-Za-z0-9]!@#$%^&*()_+=-{}':;./>?<,") Then

So, my request/question is, can someone enlighten me on the correct format to allow normal symbols, letters, and numbers, in this expression?
To be more precise, I want these symbols to be checked via the expression, along with lowercase and capital letters, and numbers 0->9.
<>?:"{}|+_)(*&^%$#@!~`-=\][';/.,

Thanks a lot!
 
The way you worded it, you said
I needed to only allow letters, numbers, and symbols in my textbox.
Isn't that what textboxes do by default? The keydown/keypress event are used to capture key inputs and you can choose to handle them - meaning suppress them. What do you mean exactly???
 
Your pattern here:
VB.NET:
[^A-Za-z0-9]!@#$%^&*()_+=-{}':;./>?<,
Basically means "match something that isn't a letter or number, followed by all these punctuation marks". Probably not what you want.

Here are 2 regex elements that may help you:

\w = any letter, number, or underscore
\p{P} = any punctuation

Applying these, this pattern should be more helpful to you:
VB.NET:
^(\w|\p{P})*$

If you're brand new to regex, then there's a whole bunch to learn. :) But I hope this gives you a good place to start.

If you want to play with the pattern I just gave you, try this link: .NET Regex Tester - Regex Storm. It's an online regex tester that will let you tweak and retest your pattern until it works exactly for your needs.
 
Back
Top