[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Regular expression

Posted on 2012-04-02
9
Medium Priority
?
433 Views
Last Modified: 2012-04-14
I need a regular validation expression for decimal with % symbol.
Example: 3.245% ,0.000% etc
numbers,dot and % only should be in the expression.

Thanks
0
Comment
Question by:KavyaVS
[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
9 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 37795880
Try:

\d+(?:\.\d+)?%

Open in new window


...and if the number to the left of the decimal is optional:

(?:\d*\.\d+|\d+(?:\.\d+)?)%

Open in new window

0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 400 total points
ID: 37795892
\d+(\.\d+)?%

If decimals part will always be there:
\d+\.\d+%
0
 
LVL 2

Assisted Solution

by:Codecaesar
Codecaesar earned 400 total points
ID: 37796277
0
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!

 

Author Comment

by:KavyaVS
ID: 37797473
number to the left of the decimal part and  % symbol is always there.

I tried this one .It is giving me error. unrecognized escape sequence.
"^[\d+(?:\.\d+)?%]".What will be the correct one.


Thanks.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 37797602
It is giving me error. unrecognized escape sequence.
C# requires backslashes within string literals to be escaped. I would suggest, however, using the @ modifier for readability.

"^[\d+(?:\.\d+)?%]".What will be the correct one.
Not that one. With that, all of the following are valid:

%
1
?
(
:
.

You've wrapped your expression in a character class ( [ ... ] ). This means all of the characters listed within the brackets are valid characters.

Here is my previous suggestion corrected for use in code:

string pattern = @"^\d+(?:\.\d+)?%";

Open in new window

0
 
LVL 6

Expert Comment

by:Sanjeev Jaiswal
ID: 37799777
As you said, number to the left of the decimal and % are  always there
which means number left to the decimal, decimal and percentage is always there

i.e. 5.0% or 123.456% or 5.%(assuming) are valid
then try something like this...
use strict;
use warnings;

my $num = "5%";
my $regex = '\d+\.\d*%';

if($num =~ m/$regex/){print "good";}
else{ print "bad";}

Open in new window

0
 
LVL 5

Assisted Solution

by:Vishal Kedar
Vishal Kedar earned 200 total points
ID: 37800315
try below expression.

\d+\.\d{3}?%

it may helps.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37800318
@jassics
That's a peculiar syntax for ASP.NET  ; )
0
 

Author Closing Comment

by:KavyaVS
ID: 37847456
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

649 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