Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Regular expression for Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regular expression for "1.0"" Watch "Regular expression for "1.0"" New topic
Author

Regular expression for "1.0"

shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
i ahve written a small set of statements to identify decimal numerals, i think the code is fine but its not working..



whats wrong with the code ? why is it not working? what will be the correct regex expression of identifying fractional values ?
Praveen Rajendran
Greenhorn

Joined: Dec 25, 2009
Posts: 21
Hi Shukla,

I guess the reason why this wud would have given you false back is because the regex defined by you does not match any character beyond the decimal.

ie it wud would have returned true if your string was "1." and not 1.0"

The below regex wud would work to match 1 or 2 decimal places.


Let me know if it helps.


Thanks & Regards,
Praveen
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Please be careful about spellings: it's not "wud" but "would".
Praveen Rajendran
Greenhorn

Joined: Dec 25, 2009
Posts: 21
Hi Campbell,

Apologies for the short words . Thanks for bringing to attention . Corrected the same with a strike through.

I am new to replying posts in Javaranch. Till date was only reading it. Thought of putting some time to help other to the way I can. Will take care going forward about spellings
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Apologies accepted
shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
well praveen, i got the answer for the same long back. In s.matches("\\d(\\.)") the regex expression applies to the entire string, i mean if the string was str = 2. or 654. then this expression would have worked out.But for a string like 2.34 the correct regex would be "\\d\\.\\d+" now it will work.

Anyways thanks for your effort.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Regular expression for "1.0"
 
Similar Threads
question on expressions
JSTL not working
Calculating Interest Payments
Error coming while using c:out tag in JSTL
how this expression works. "/\s/g"