0

I would need a regular expression that treats numbers with dots and commas the same way. Commas should not be treated as thousand separators.

Example:
9.8
9,8

should return 9.8 both

I tried this, but it doesn't work.

^(\d+(?:[\.\,]\d{2})?|)$ 
NoobEditor
  • 15,563
  • 19
  • 81
  • 112

3 Answers3

1

Your regex ^(\d+(?:[\.\,]\d{2})?|)$ matches numbers that have exactly 2 decimals, if you want to match any number (1 or more) of decimals, use:

^(?:\d+(?:[.,]\d+)?|)$ 
Toto
  • 89,455
  • 62
  • 89
  • 125
0

try this :

^(\d*[.,]?\d*)*$

demo here : http://regex101.com/r/bE4iS3

aelor
  • 10,892
  • 3
  • 32
  • 48
0

PHP

$re = '/(,)/mx'; 
$str = '9.8 \n9,8'; 
$subst = '.'; 
preg_replace($re, $subst, $str);

JAVASCRIPT

var re = /(,)/gmx; 
var str = '9.8 \n9,8';
var subst = '.'; 
str.replace(re, subst);

PYTHON

import re
p = re.compile(r'(,)', re.MULTILINE | re.VERBOSE)
str = "9.8 \n9,8"
subst = "." 
re.sub(p, subst, str)
MattSizzle
  • 3,145
  • 1
  • 22
  • 42