Regular Expression Text

S

sianan

Hi,

I need to create a Regular Expression that will allow the user to enter
either apaha-numeric characters (a-z, A-Z, 0-9) or extended characters
(any extended character) (!@#$%^&*()) into a text field.

The alpha-numeric part is easy, it's the extended characters I'm having
trouble with. Does anybody have an example?

Thanks!
 
M

Michael Nemtsev

Hello sianan,

AFAIK (nowhere to check), the \W is responsible for the symbols except a-z,
A-Z, 0-9
try to play with it

s> Hi,
s>
s> I need to create a Regular Expression that will allow the user to
s> enter either apaha-numeric characters (a-z, A-Z, 0-9) or extended
s> characters (any extended character) (!@#$%^&*()) into a text field.
s>
s> The alpha-numeric part is easy, it's the extended characters I'm
s> having trouble with. Does anybody have an example?
s>
s> Thanks!
s>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
K

Kevin Spencer

It sounds to me like you'd be better off making a rule about what the user
can *not* put into the text box. I can't figure this out from what you've
posted.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
 
M

Mark Wilden

It sounds to me like you'd be better off making a rule about what the user
can *not* put into the text box. I can't figure this out from what you've
posted.

The user should not be able to enter a character that's not a-z, A-Z, 0-9,
!, @, #, $, %, ^, &, *, (, or ) .

This could be tested using

new Regex(@"^[\w\d!@#$%^&*()]+$").IsMatch(stringToTest);
 
K

Kevin Spencer

Okay, it was your use of the term "extended characters" that was confusing.
The user should not be able to enter a character that's not a-z, A-Z, 0-9,
!, @, #, $, %, ^, &, *, (, or ) .

[^\w!^@#$%&*()]

This will do it. This is a character class that indicates that any character
*not* matching the characters you specified is a match. Therefore, if you
have a match, you have an illegal character in the string.

Note that it uses the shorthand "\w" ("word" characters - digits or
alphabetical characters). It does *not* include spaces. You would have to
add spaces to it for that:

[^\w\s!^@#$%&*()] (using the "\s" character shorthand for space characters)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.


Mark Wilden said:
It sounds to me like you'd be better off making a rule about what the
user can *not* put into the text box. I can't figure this out from what
you've posted.

The user should not be able to enter a character that's not a-z, A-Z, 0-9,
!, @, #, $, %, ^, &, *, (, or ) .

This could be tested using

new Regex(@"^[\w\d!@#$%^&*()]+$").IsMatch(stringToTest);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regular Expression Help 1
Regular expression 5
Regular expression 4
Regular Expression Help 3
regular expressions 5
Simple regular expression for length 2
regular expression 7
Problem with regular expression 3

Top