正则表达式检查数字系统

huangapple go评论94阅读模式
英文:

Regular expression to check number system

问题

正则表达式匹配数字系统1:

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

正则表达式匹配数字系统2:

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

正则表达式匹配数字系统3:

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

这些正则表达式可以用于匹配不同数字系统的数字,包括千位分隔符和小数点。

英文:

I am working on an application that takes numbers and converts them to the locale format. In order to do so I must first identify which locale the entered number belongs to format it accordingly.

Therefore, can anybody please provide Regex to match which number system the given number belongs too?

Example:

Regex for number system 1:

  • 1 // should match
  • 10 // should match
  • 1000 //should match
  • 1000.00 //should match
  • 1,000.00 //should match

And so on..

Regex for number system 2:

  • 1 // should match
  • 10 // should match
  • 1000 // should match
  • 1.000 // should match
  • 1.000,00 // should match

And so on..

Regex for number system 3:

  • 1 // should match
  • 10 // should match
  • 1000 // should match
  • 1 000 // should match
  • 1 000.00 // should match.

And so on..

So far I have written this: /^\d*[,]?\d*\.?\d+$/ But this doesn't work if there are more than one thousand separators. Example: 10,00,000.00

I basically need three regex to check which of the 3 widely used number system the entered number belongs to.

答案1

得分: 1

  1. const commaDot = /^(?:\d{0,3}(?:,\d{3})|\d+)(?:\.\d+)?$/
  2. const dotComma = /^(?:\d{0,3}(?:.\d{3})|\d+)(?:,\d+)?$/
  3. const spaceDot = /^(?:\d{0,3}(?: \d{3})|\d+)(?:.\d+)?/
  4. const cases = [
  5. '1',
  6. '10',
  7. '1000',
  8. '1000.00',
  9. '1,000.00',
  10. '1',
  11. '10',
  12. '1000',
  13. '1000,00',
  14. '1.000,00',
  15. '1',
  16. '10',
  17. '1000',
  18. '1000.00',
  19. '1 000.00',
  20. ]
  21. for (const cas of cases) {
  22. console.log(
  23. cas,
  24. 'matches:',
  25. cas.match(commaDot) ? '1' : '',
  26. cas.match(dotComma) ? '2' : '',
  27. cas.match(spaceDot) ? '3' : ''
  28. )
  29. }
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const commaDot = /^(?:\d{0,3}(?:,\d{3})|\d+)(?:\.\d+)?$/
  2. const dotComma = /^(?:\d{0,3}(?:\.\d{3})|\d+)(?:,\d+)?$/
  3. const spaceDot = /^(?:\d{0,3}(?: \d{3})|\d+)(?:\.\d+)?$/
  4. const cases = [
  5. &#39;1&#39;,
  6. &#39;10&#39;,
  7. &#39;1000&#39;,
  8. &#39;1000.00&#39;,
  9. &#39;1,000.00&#39;,
  10. &#39;1&#39;,
  11. &#39;10&#39;,
  12. &#39;1000&#39;,
  13. &#39;1000,00&#39;,
  14. &#39;1.000,00&#39;,
  15. &#39;1&#39;,
  16. &#39;10&#39;,
  17. &#39;1000&#39;,
  18. &#39;1000.00&#39;,
  19. &#39;1 000.00&#39;,
  20. ]
  21. for (const cas of cases) {
  22. console.log(
  23. cas,
  24. &#39;matches:&#39;,
  25. cas.match(commaDot) ? &#39;1&#39; : &#39;&#39;,
  26. cas.match(dotComma) ? &#39;2&#39; : &#39;&#39;,
  27. cas.match(spaceDot) ? &#39;3&#39; : &#39;&#39;
  28. )
  29. }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 04:20:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定