Solved

Regular expression

Posted on 2012-04-02
9
383 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
9 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 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 100 total points
ID: 37795892
\d+(\.\d+)?%

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

Assisted Solution

by:Codecaesar
Codecaesar earned 100 total points
ID: 37796277
0
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…

912 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now