• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java Regex for Decimal values

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephen. This works now.


@Campbell. Thanks. Just looked at how scanner.nextDouble() works.
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Darryl. Yes you are right. Pipe character isn't necessary. Thanks
 
a wee bit from the empire
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic