Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on 

Regular Expression to exclude some special characters

Hi,

I am working on C# and ASP.Net1.1

I require a reqular expression which can accept any character other than

single quote('), double quote("), @, %,#

Can anyone help me in this

Thanks
Regular ExpressionsC#

Avatar of undefined
Last Comment
ddrudik
Avatar of ddrudik
ddrudik
Flag of United States of America image

^[^'"@%#]+$

Double the ' or " depending on how you enclose the pattern in code.
Avatar of abel
abel
Flag of Netherlands image

You could use a character class. The regex would look like:
['"@%#]

In C# that would be:

System.Text.RegularExpressions.Regex.IsMatch("test", "['\"@%#]")

Open in new window

Avatar of abel
abel
Flag of Netherlands image

Note that there's a slight difference between ddrudik's and my attempts: mine will test true if any of your mentioned characters hit, wherever in the string. Ddrudik's will match when all characters are not of the set you mentioned.

In terms of performance they may differ a bit, but in ASP, I wouldn't bother too much about that.
Avatar of ddrudik
ddrudik
Flag of United States of America image

>I require a reqular expression which can accept any character other than...
That is what my offered solution was constructed to match.  It will only match true if all characters in the string tested are not a ',",@,% or #.
Avatar of abel
abel
Flag of Netherlands image

:)
I didn't mean to say you were wrong. They are two approaches to the same requirement.
Avatar of ddrudik
ddrudik
Flag of United States of America image

No worries here.
Avatar of tia_kamakshi
tia_kamakshi
Flag of United Arab Emirates image

ASKER

Thanks all
ddrudik's expression works great

Can you please help me in understanding this

^[^'"@%#]+$

What is +$ at the end means

Thanks again to all for co-operation
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ddrudik
ddrudik
Flag of United States of America image

Using * instead of + would match 0 or more times, which would allow an empty string.  + requires that the string be at least 1 character in length.
Avatar of abel
abel
Flag of Netherlands image

In case you want to know more, as a good starting point (and to a certain extend also for advanced users): http://www.regular-expressions.info/ is a good place to be...
Avatar of tia_kamakshi
tia_kamakshi
Flag of United Arab Emirates image

ASKER

Thanks
Avatar of ddrudik
ddrudik
Flag of United States of America image

Thanks for the question and the points.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo