Solved

Regular expression

Posted on 2012-04-02
9
374 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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

706 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

14 Experts available now in Live!

Get 1:1 Help Now