正则表达式匹配德国数字 [英] Regular expression to match German number

查看:487
本文介绍了正则表达式匹配德国数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在德语中,逗号用作十进制标记,点号用来表示德国文化的正确格式。用来分隔数千人。



因此:


  • 1.000等于1000

  • 1,000等于1
  • 1.000,89等于1000.89
  • 1.000.123.456,89等于1000123456.89



在我看来,真正的技巧是确保可以有多个点,

解决方案

这是我使用的正则表达式:

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

p>

Debuggex Demo



和t他是一个代码示例,将其解释为一个有效的浮点数(在字符串替换之后注意 parseFloat())。



  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('\\\
'); code>

< textarea id =outrows =10style =width:100%>< / textarea>

I am wondering, how would regular expression for testing correct format of number for German culture would look like.

In German, comma is used as decimal mark and dot is used to separate thousands.

Therefore:

  • 1.000 equals to 1000
  • 1,000 equals to 1
  • 1.000,89 equals to 1000.89
  • 1.000.123.456,89 equals to 1000123456.89

The real trick, seems to me, is to make sure, that there could be several dots, optionally followed by comma separator

解决方案

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>

这篇关于正则表达式匹配德国数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆