Regular expression to match German number

只谈情不闲聊 提交于 2019-11-28 01:36:30

This is the regex I would use:

^-?\d{1,3}(?:\.\d{3})*(?:,\d+)?$ 

Debuggex Demo

And this is a code example to interpret it as a valid floating point (notice the parseFloat() after the string replacements).

var numbers = ['1.000', '1,000', '1.000,89', '1.000.123.456,89'];    document.getElementById('out').value=numbers.map(function(str) {    return parseFloat(str.replace(/\./g, '').replace(',', '.'));  }).join('\n');
<textarea id="out" rows="10" style="width:100%"></textarea>

This regex should work :

([0-9]{1,3}(?:\.[0-9]{3})*(?:\,[0-9]+)?) 

I would have posted this as a comment, but I dont have enough reputation. @funkwurm, your post https://stackoverflow.com/a/28361329/7329611 contains javascript

var numbers = ['1.000', '1,000', '1.000,89', '1.000.123.456,89', '1.2']; numbers.map(function(str) {   return parseFloat(str.replace(/\./g, '').replace(',', '.')); }).join('\n'); 

which should convert german numbers to english/international ones - which it does for every number with exactly three digits after a german thousands dot like the numbers you use in the example array. BUT - and there is the critical Use-Case-Error: it just deletes dots from any other string with not three digits after it aswell. So if you insert a string like '1.2' it returns 12, if you insert '1.23' it returns 123. And this is a very critical behaviour, if anyone just takes the above code snippet and thinks it'll convert any given number correctly into english ones. Because already correct english numbers will be corrupted! So be careful, please.

A good regex would be something like this

Regex regex = new Regex("-?\d{1,3}(?:\.\d{3})*(?:,\d+)?"); Match match = regex.Match(input); Decimal result = Decimal.Zero; if (match.Success)      result = Decimal.Parse(match.Value, new CultureInfo("de-DE")); 

The result is the german number as parsed value.

Try this it will match your inputs:

^(\d+\.)*\d+(,\d+)? 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!