• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 13396
296
  • 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: 74376
334
  • 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: 74376
334
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic