Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Positive number match with regular expression in java

Posted on 2010-09-17
4
Medium Priority
?
700 Views
Last Modified: 2012-05-10
Hi There,

I am trying to match ONLY positive number of type Double with a regular expression in Java.

any clues?

Thanks.
0
Comment
Question by:theartha
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 33703194
Try matching the following:
	final String NO_NEG = "(?<!-)\\d*\\.\\d+";

Open in new window

0
 

Author Comment

by:theartha
ID: 33703260
@CEHJ:

How to compare that NO_NEG string with my input value.?

i.e
Pattern pattern = Pattern.compile(NO_NEG);
Matcher matcher1 = pattern.matcher(amount);  
matcher1.matches()== true{
//do something
}

Error: The method matcher(CharSequence) in the type Pattern is not applicable for the arguments
 (Double)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33703314
Simply
boolean ok = amount.matches(NO_NEG);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33703396
:-)
0

Featured Post

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!

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,…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

824 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