Solved

Regular expression help

Posted on 2014-11-12
7
99 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: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

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

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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.

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 75

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What can cause the styling on a .NET site to not be found? 17 24
HttpPostedFile 1 26
List<PaisEntity> - show some 1 24
Can I command line build a C# application? 6 30
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

786 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