正则表达式在Go中不匹配。

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

Regex Go mismatch

问题

我在regexr中开发了一些正则表达式,它们按预期工作,但是当我在Go中使用它们时,似乎字符串不匹配。

(+|-)?(((\d{1,3}[, ])(\d{3}[ ,])\d{3})|\d+)( ?[.,] ?(\d{3}[, ])\d+)?

例如,在regexr中,以下输入不匹配:

1.12,4.64

但在Go中,它是匹配的。

英文:

I have developed some regex in regexr where it works as expected, but when I use it in Go it seems to be mismatching strings.

(\+|-)?(((\d{1,3}[, ])(\d{3}[ ,])*\d{3})|\d+)( ?[\.,] ?(\d{3}[, ])*\d+)?

For example in regexr the following input does not match:

1.12,4.64

But in Go it does match.

答案1

得分: 5

这是一个正则表达式,用于匹配数字的千位分隔符。它可以匹配以下格式的数字:

  • 以正号或负号开头(可选)
  • 整数部分可以有千位分隔符(逗号或空格),每三位一个分隔符
  • 整数部分可以没有分隔符
  • 可以有小数部分,小数点前后可以有空格
  • 小数部分可以有千位分隔符,每三位一个分隔符

你可以在这个链接上测试这个正则表达式:https://regex101.com/r/qH1uG3/4

英文:
^(\+|-)?(((\d{1,3}[, ])(\d{3}[ ,])*\d{3})|\d+)( ?[\.,] ?(\d{3}[, ])*\d+)?$

Try with anchors.^$ will disable partial matching.See demo.

https://regex101.com/r/qH1uG3/4

huangapple
  • 本文由 发表于 2015年2月11日 20:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/28454756.html
匿名

发表评论

匿名网友

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

确定