11

I need a regular expression for 6 digit number with optional 2 decimal digit Allowed values:

    .1  .11  0.11  10. 10.1   10.12  00.00  0.0  00.00

   123456 12345 1234 123 12 1 0 0. 123. 123.55 123456.  
Kara
  • 6,115
  • 16
  • 50
  • 57
Swapnil
  • 151
  • 1
  • 3
  • 9

4 Answers4

13

Regex should be: ^\d{0,6}(\.\d{0,2})?$(It passed all of your samples)

Update:

To avoid empty string and single dot, regex is ^(?!\.?$)\d{0,6}(\.\d{0,2})?$. The expression adds a negative lookahead ?!\.?$, which excludes 0 or 1 dot.

I added a unit test on Fiddle.

Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
5

Let's break it into four regexes. At least one of these four must match.

# decimal, 1-2 digits
\.\d{1,2}

# 1-4 digits, optional decimal, 0-2 digits
\d{1,4}\.?\d{0,2}

# 5 digits, optional decimal, optional digit
\d{5}\.?\d?

# 6 digits, optional decimal
\d{6}\.?

Which can then be combined into a single regex with the alternation operator (|):

(\.\d{1,2}|\d{1,4}\.?\d{0,2}|\d{5}\.?\d?|\d{6}\.?)

Then add a caret (^) and stick ($) to match the beginning and end of the string.

^(\.\d{1,2}|\d{1,4}\.?\d{0,2}|\d{5}\.?\d?|\d{6}\.?)$

This doesn't scale very well (e.g. if you wanted to match 100 digits with up to 20 after the decimal point) but it works and it's relatively easy to understand.

If you don't have to use a regex, there are easier ways to solve this problem. :)

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
2

This r.e. is matching all your examples, and does not accept more than 6 digits or 2 decimals.

^\d{0,5}(\d\.\d?|\.\d)?\d?$
Teudimundo
  • 2,610
  • 20
  • 28
0

Quick java example for validating string with max length as 10 i.e. 9 digits with max 3 decimal NOTE: why 9 digits instead of 10 digits? because the decimal point also count as a character

package com.baji.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

/*
 * @author: Baji Shaik
 * Quick java example for validating string with max length as 10
 * i.e. 9 digits with max 3 decimal
 * NOTE: why 9 digits instead of 10 digits? because the decimal point also count as a character
*/
public class CheckNumber {


  @Test
  public void testSimpleTrue() {
    String s= "12345678.1";
    assertTrue(Validate10DigitsWithMax3DecimalsNumber(s));
    s= "1234567.12";
    assertTrue(Validate10DigitsWithMax3DecimalsNumber(s));
    s = "123456.123";
    assertTrue(Validate10DigitsWithMax3DecimalsNumber(s));
    s = "123456789";
    assertTrue(Validate10DigitsWithMax3DecimalsNumber(s));

    s = "1234567891";
    assertTrue(Validate10DigitsWithMax3DecimalsNumber(s));
    s = ".123";
    assertTrue(Validate10DigitsWithMax3DecimalsNumber(s));

    s = "000000.123";
    assertTrue(Validate10DigitsWithMax3DecimalsNumber(s));

  }


  public static boolean Validate10DigitsWithMax3DecimalsNumber(String value){
    Pattern pattern = Pattern.compile("^(\\.\\d{1,3}|\\d{1,6}\\.\\d{1,3}|\\d{1,7}\\.\\d{1,2}|\\d{1,8}\\.\\d{1}|\\d{1,10})$");
    Matcher matcher = pattern.matcher(value);
    if (matcher.find()){
      return true; 
    } 
    return false; 
  }

} 
David Waters
  • 11,979
  • 7
  • 41
  • 76
Baji Shaik
  • 245
  • 1
  • 4
  • 11