开发者

Regular expression for Validation of data types like amount, decimal, custom decimal?

What is regular expression for Decimal,Amount,CustomDecimal datatype required for validation ?

My xml records contains few values in string format with data types as Decimal,Amount,CustomDecimal datatype .

I am using GWT celltable : EditTextCell Parsed xml set to cells in table. Now I want to validate that cell with related data type.

Values in string format so I want to match that with regular expression

Following is my criteria for regular expression for different datatype:

Integer Can have a maximum scale of 8. ==> (-)?(\d){1,8}

Decimal The number can be represented as (p,s) where p is the precision and s is the scale and then describe everything with respect to precision and scale. Can have a maximum scale of 8 and a maximum precision of 31. The non-fractional part of the number can have a maximum of 23 digits. --> ?

Amount This can have a maximum scale of 8 and a maximum precision of 31. --> ?

Custom Decimal The number can be represented as (p,s) where p is the precision and s is the scale and then describe everything with respect to precision and scale. Use Custom Decimal if you need to store decimals that have more than eight digits in the fractional part. Can have开发者_运维知识库 a maximum scale of 29 and a maximum precision of 30. The non-fractional part of the number can have a maximum of 29 digits. --> ?

Any help or guidance in this matter would be appreciated.


Integer

You don't need the brackets as long as you don't want to have the content in a capturing group. So I assume this would be fine for you:

-?\d{1,8}

oh and you have to use anchors to ensure that the complete string matches your pattern and not only a substring.

^-?\d{1,8}$

^ matches the start of the string

$ matches the end of the string

Decimal

Try something along

^-?\d{1,23}\.\d{1,8}$

The open questions here are:

  • Is something optional, that means would some of those formats be allowed: 1. or .1 or only 1?

  • What fraction delimiter .or , or both?

If you want to make a part optional put it into a non capturing group and put a ? behind it((?:Pattern)?)

To allow both delimiter use a character class like this [.,] instead of the \. part.

Amount

Is it different to Decimal?

Custom Decimal

If I understand it correct, then it could have a max length of 30. And both parts before and after the dot can be max 29. I assume then when my non fractional part is 29, then my fraction can be max 1.

So I would keep it along the decimal regex and add an additional length check using a lookahead assertion (hoping your regex engine supports it)

(?=^.{1,30}$)^-?\d{1,23}\.\d{1,8}$

This lookahead assertion (?=^.{1,30}$) checks at first if the whole string is between 1 and 30 characters long, if true then it checks the pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜