Regular expression help

Posted on 2014-11-12
Last Modified: 2014-11-14
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.
Question by:AlHal2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 14

Expert Comment

by:Vel Eous
ID: 40437214
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


Author Comment

ID: 40437394
There are some non alpha numeric characters that are OK eg colons, dollar signs, pound signs, ampersands and others.
LVL 14

Expert Comment

by:Vel Eous
ID: 40437927
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40438522
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


Author Comment

ID: 40440655

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.
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 40440668
Change the \" to "":

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

Open in new window


Author Closing Comment

ID: 40442201

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL ( and MongoDB (…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question