?
Solved

Regular expression help

Posted on 2014-11-12
7
Medium Priority
?
107 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
[X]
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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 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