?
Solved

Regular expression

Posted on 2012-04-02
9
Medium Priority
?
427 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
Independent Software Vendors: 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 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