【发布时间】:2019-09-17 19:41:33
【问题描述】:
我正在尝试使用 Golang 的正则表达式将某个字符串类型拆分为其组成部分。
我所拥有的是任何给定浮点数的Sprinf(".2f", n)(将其简化为小数点后 2 位),并且希望将百分位数字分开,例如:
"1.25" = ["1.2", "5"]
"1.99" = ["1.9", "9"]
在 PHP 中,这类似于:
preg_match('/^ (\-? \d [.] \d) (\d) $/x', sprintf('%1.2f', $input), $matches)
我可以通过 $matches[0] 和 $matches[1] 获得零件。
试过了:
re := regexp.MustCompile(`/^ (\-? \d [.] \d) (\d) $/x`)
fmt.Printf("%q\n", re.FindAllStringSubmatch("1.50", 2))
无济于事。提前致谢。
【问题讨论】:
-
你试过没有空格吗?
^(\-?\d[.]\d)(\d)$见regex101.com/r/8rLiLk/2 -
感谢 mate.. 用 regexp.MustCompile(
^(\-?\d[.]\d)(\d)$) 尝试过,它可以工作.. -
x不是 RE2 中支持的标志:github.com/google/re2/wiki/Syntax#Flags