Solved

Regular expression

Posted on 2012-04-02
9
412 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 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
Industry Leaders: 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 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

Industry Leaders: 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!

Question has a verified solution.

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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…

696 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