Hi Folks,
I have been searching to find a reglular expression that deals with decimal points, they are not mentioned on the Sun Tutorials for Regular Expressions. Basically im trying to verify that a string taken in from a screen matches the format of (15).99. This is not for my homework assignment:-) If anyone even knows of a good tutorial that covers this, that would be great as im kinda new to Java development.

Thanks

Man In A Can

Dani AI

Generated

Building on ’s regex tip and ’s stepwise checks, here is a tighter pattern if your intent is: up to 15 digits before the dot, exactly 2 digits after, whole string match, optional sign, and no leading zeros (except the single 0 case like 0.99).

// ^ anchor, optional sign, integer (0 or non-zero followed by up to 14 digits), dot, exactly 2 decimals, $ anchor
Pattern p = Pattern.compile("^[+-]?(?:0|[1-9]\\d{0,14})\\.\\d{2}$");
boolean ok = p.matcher(input).matches();

Why this helps:

  • Anchors (^ and $) ensure you do not match inside a longer string.
  • Escaping the dot (\.) is required; a bare dot matches any character.
  • {1,15} on the integer portion would allow leading zeros; the non-capturing group (?:0|[1-9]\\d{0,14}) avoids values like 00.99 while still allowing 0.99.
  • Exactly two decimals are enforced by \\d{2}.

Variations you might need:

  • Allow inputs like .99 (no integer part): make the integer portion optional while still banning leading zeros when present:

    ^[+-]?(?:(?:0|[1-9]\\d{0,14}))?\\.\\d{2}$

  • If you are validating, then also parsing, consider a second check with BigDecimal for range rules (e.g., max value) after the regex passes. Regex is great for format; parsing is better for numeric constraints.

Locale note: if the decimal separator is not a dot, read it from DecimalFormatSymbols and splice it into the pattern with Pattern.quote(...) so your validation remains locale-aware.

Recommended Answers

All 7 Replies

1. check for the number of decimal points in the input. if this is not 1 => false
2. check that everything before the decimal point is a decimal. if not => false
3. check that everything after the decimal point is a decimal if not so=> false
4. check the number of decimals after the decimal pont. if not two => false

if all these checks cleared, you've got a match. don't know whether or not regex has a quick sollution for it, but you could as well do it without

Cool Thanks very much, i actually worked it out, i had been using "[0-9]{0,15}.{1}[0-9]{2}" and i have since found that this "[0-9]{0,15}[.]{1}[0-9]{2}" will work.

Thanks

Man In A Can

You can simplify it just a little more with a digit character match like so

\d{0,15}\.\d{0,2}

You might find this small regex testing form that I posted a while back helpful in tweaking patterns as you are experimenting: http://www.daniweb.com/forums/post392585-8.html
The numbered text fields at the bottom are the match group results of the pattern.

Thanks very much for all your help.

I would go with the following logic:

if no integer portion is provided, check if decimal is given along with atleast one fractinal portion of the decimal, also forces user to give atleast one digit
to avoid the empty string NumberFormatException when user provides ""
OR
if a single integer portion is provided, you may or may not give a fraction portion with or without a decimal "."
match the precision and scale first as a decimal number, pretty much solves avoiding NumberFormatException

if(Pattern.compile("^(\\d{0," + maxIntegralDigits + "}(\\.\\d{1," + maxFractionalDigits + "}))+|(\\d{1," + maxIntegralDigits + "}(\\.\\d{0," + maxFractionalDigits + "})?)$").matcher(fieldValue).matches()

which turns out to be

^(\\d{0,18}(\\.\\d{1,6}))+|(\\d{1,18}(\\.\\d{0,6})?)$

for an (18,6) (precision+scale, scale) representation

I would go with the following logic:

if no integer portion is provided, check if decimal is given along with atleast one fractinal portion of the decimal, also forces user to give atleast one digit
to avoid the empty string NumberFormatException when user provides ""
OR
if a single integer portion is provided, you may or may not give a fraction portion with or without a decimal "."
match the precision and scale first as a decimal number, pretty much solves avoiding NumberFormatException

if(Pattern.compile("^(\\d{0," + maxIntegralDigits + "}(\\.\\d{1," + maxFractionalDigits + "}))+|(\\d{1," + maxIntegralDigits + "}(\\.\\d{0," + maxFractionalDigits + "})?)$").matcher(fieldValue).matches()

which turns out to be

^(\\d{0,18}(\\.\\d{1,6}))+|(\\d{1,18}(\\.\\d{0,6})?)$

for an (18,6) (precision+scale, scale) representation

Atleast check the dates while posting, the last post on this thread is more than an year old !!!

Thank you Man In A Can. You saved my day

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.