Check RegEx decimal numbers with semicolon or period

How to check such a single-register number entry. Lines are not allowed. Two decimal places after a dot or comma.

Example:

123.34
1.22
3.40
134.12
123

+6
source share
3 answers

Try this regex:

/^(\d+(?:[\.\,]\d{2})?)$/ 

If $1 exactly matches your input string, then suppose it is validated.

+9
source
 pat = re.compile('^\d+([\.,]\d\d)?$') re.match(pat, '1212') <_sre.SRE_Match object at 0x91014a0> re.match(pat, '1212,1231') None re.match(pat, '1212,12') <_sre.SRE_Match object at 0x91015a0> 
+2
source

Try it,

 /^(\d+(?:[\.\,]\d{1,2})?)$/ 
+2
source

Source: https://habr.com/ru/post/900020/


All Articles