Regular expression for a 6-digit number with optional 2 decimal digits

I need a regular expression for a 6-digit number with an optional 2-digit number Valid 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. 
+8
regex stringtokenizer
source share
4 answers

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

Update:

To avoid an empty line and a single dot, the regular expression is ^(?!\.?$)\d{0,6}(\.\d{0,2})?$ . The expression adds a negative lookahead ?!\.?$ , Which excludes 0 or 1 point.

I added unit test to Fiddle .

+10
source share

Let me break it down into four regular expressions. At least one of the 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 one regular expression like this.

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

Then add the carriage ( ^ ) and insert ( $ ) at the beginning and end of the line.

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

It doesn't scale very well (for example, if you want to match 100 digits with an accuracy of 20 after the decimal point), but it works, and it's relatively easy to understand.

If you don't need regular expression, there are simpler ways to solve this problem. :)

+4
source share

This re matches all of your examples and does not accept more than 6 digits or 2 decimal places.

 ^\d{0,5}(\d\.\d?|\.\d)?\d?$ 
+2
source share

A quick java example for checking a string with a maximum length of 10 i.e. 9 digits with a maximum value of 3 decimal NOTE: why 9 digits instead of 10 digits? because the decimal point is also considered a symbol

 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 * ie 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; } } 
0
source share

All Articles