• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Java Regex for Decimal values

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've writing a regex for validating decimal values.

These are the conditionals:
Decimal values can have a + or -
The value can have either a dot or a comma as the decimal separator
The maximum number of digits before the decimal separator is 8
The maximum number of digits after the decimal separator is 3

This is the regex i've written but it somehow doesn't seem to work..I'm writing this inside a Inputfilter in android and matcher.matches() returns true for 999999999 etc.

Pattern mPattern = Pattern.compile("[\\+\\-]?[0-9]{0,8}[,|.]?[0-9]{0,3}");

What is the problem with this regex?

Thank you
 
Saloon Keeper
Posts: 11471
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It matches 8 digits, then it matches no separator, and then it matches another digit. The separator should not be optional if there's a fractional part.
 
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in the documentation for java.util.Scanner and you will find some regexes there.
 
Sekhar Nat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephen. This works now.


@Campbell. Thanks. Just looked at how scanner.nextDouble() works.
 
Campbell Ritchie
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the pipe character between the comma and dot in that character class. Not unless the pipe character (which is not a metacharacter inside a character class) is also acceptable as a decimal delimiter.

As it stands, your regex will match "9999|99" which is probably not what you want.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sekhar Nat wrote:Thanks Stephen. This works now.



I don't see why the \\ pairs are needed?
 
Sekhar Nat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is not necessary. [+-] should do good. Take a look at this.
 
Sekhar Nat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Darryl. Yes you are right. Pipe character isn't necessary. Thanks
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!