2

I need a regular expression that can validate Decimal (18,3), meaning a precision of 18, and a scale of 3.

It would need to pass the following criteria:

  • Max number of digits before comma is 15
  • Max number of digits after the comma is 3

Valid Examples:

123456789.123
123456789123456.12
12345.1
123456789123456

Not Valid Examples:

1234567891234567
123.1234
1.12345
.1234

How can I achieve this?

user2828076
  • 41
  • 1
  • 3

3 Answers3

4

First of all, the character . is called a dot, or period, or full stop, or decimal point, while a comma is the character: ,.

Second, you can designate a digit in regex in a character class: [0-9] means any digit between 0 to 9.

A dot in regex will match any character, so you need to escape it by means of a backslash (or putting it in a character class).

Remember though that the elements in the same character class can be in any order, so that if you want to get something having digits and a dot and use [0-9.] it will match any digit, or a dot.

Now, you will need finite quantifiers for your task, which are designated by braces in the form of {m,n} where m is the minimum and n the maximum. If you now use... [0-9.]{1,15} it would mean the character class [0-9.] repeated 1 up to 15 times.

But if you use [0-9.]{1,15}, it will also match ...... or 1234 (remember I said earlier that the order did not matter.

So, applying all this, you get the regex:

[0-9]{1,15}\.[0-9]{1,3}

Now, since you are doing a validation, you will need anchors to specify that the regex should test the whole string (instead of simply finding a match).

^[0-9]{1,15}\.[0-9]{1,3}$

Last, since you can have optional decimals, you will have to use a group and the quantifier ? which means 0 or 1:

^[0-9]{1,15}(?:\.[0-9]{1,3})?$

In your code, you will create the regex a bit like this:

string myString = "123456789.123";
var regexp = new Regex(@"^[0-9]{1,15}(?:\.[0-9]{1,3})?$");
var setMatches = regexp.Matches(myString);
foreach (Match match in setMatches)
{
    Console.WriteLine(match.Groups[0].Value);
}

This will output the decimal if it passed the regex.

Or something like this:

string myString = "123456789.123";
Match match = Regex.Match(myString, @"^[0-9]{1,15}(?:\.[0-9]{1,3})?$");
if (match.Success)
{
    Console.WriteLine(match.Groups[0].Value);
}
Jerry
  • 70,495
  • 13
  • 100
  • 144
  • thanks a lot [0-9]{1,15}\.[0-9]{1,3} is working fine but i have to enter 3 digits after DOT , can i make it optional to take the digits after dot ? – user2828076 Sep 29 '13 at 11:13
  • @user2828076 Oh, right, sure. I actually missed that part ^^; I'll add it to my answer in a bit – Jerry Sep 29 '13 at 11:18
  • @user2828076 Done, I've edited that part in my answer. – Jerry Sep 29 '13 at 11:19
1

This regex should work for you:

/^\d{1,15}(\.\d{1,3})?$/

In Java:

Pattern p = Pattern.compile("^\\d{1,15}(\\.\\d{1,3})?$");
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

try this

  String str="Your input";
    Pattern pattern = Pattern.compile("^\\d{1,15}($|(\\.\\d{1,3}$))");
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        System.output.Println(matcher.group());
    }

in c# try this

    MatchCollection mc = Regex.Matches("your text", "^\d{1,15}($|(\.\d{1,3}$))");
             foreach (Match m in mc)
             {
                Console.WriteLine(m);
            }
Rijo Joseph
  • 1,375
  • 3
  • 17
  • 33