Solved

Regular expression help

Posted on 2014-11-12
7
93 Views
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.
;
,
<
>
\
"
|
[
]
{
}
$
+
-
.
_
(
0
Comment
Question by:AlHal2
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:Tchuki
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

0
 

Author Comment

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

Expert Comment

by:Tchuki
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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40438522
You can pretty much throw those into a character class and be done with it.

e.g.

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

Open in new window

0
 

Author Comment

by:AlHal2
ID: 40440655
Kaufmed,

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

Tchuki,

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.
0
 
LVL 74

Accepted Solution

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

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

Open in new window

0
 

Author Closing Comment

by:AlHal2
ID: 40442201
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now