Regular expression help

A user is entering information into a textbox.  The textbox should not contain any of these characters.  Please could I have a regular expression to highlight these?  I'm writing a web based program in c# using visual studio 2008.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vel EousResearch & Development ManagerCommented:
The following should do what you are requesting, although it is slightly different in that it matches all non alphanumeric characters.

var strRegex = @"\S+(?<=[\W_])";
var myRegex = new Regex(strRegex, RegexOptions.None);
var illegalChars = @"The rain in space ! peter _ -   [ {}]@'?/>|\      < > ! _ ""£%" + "\n\n" + @";<>";
var noIllegalChars = "Hello world";

var regexMatch1 = myRegex.IsMatch(illegalChars);
var regexMatch2 = myRegex.IsMatch(noIllegalChars);

Open in new window

AlHal2Author Commented:
There are some non alpha numeric characters that are OK eg colons, dollar signs, pound signs, ampersands and others.
Vel EousResearch & Development ManagerCommented:
Probably not the neatest expression but it will capture the characters you provided in your OP (excluding the $ character and including the closing parenthesis):

string strRegex = @"(([^\s\w]|[_])(?<=[;,<>\\""|\[\]{}\+-._\(\)]))";

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

kaufmed 👽Commented:
You can pretty much throw those into a character class and be done with it.


if (Regex.IsMatch(textBox1.Text, @"[;,<>\"|\[\]{}$+\-._(]"))
    // Bad character found
    // Text is good

Open in new window

AlHal2Author Commented:

With your syntax I got an error that a ) was missing.


With that expression everything was being flagged.  Would it be possible for either expert to provide full code.  I don't really know regex at all.
kaufmed 👽Commented:
Change the \" to "":

if (Regex.IsMatch(textBox1.Text, @"[;,<>""|\[\]{}$+\-._(]"))

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.