如何改进Golang中的分割逻辑

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

How to improve a split logic in golang

问题

将以下示例作为索引的可能值:

values := [5]string{"32.5ms", "32.5 ms", "32.5%", "32.5 %", "none"}

请注意,原始值可能带有空格,也可能没有度量单位的空格(32.5%,32.5 %,32.5 %等)。

我需要从原始值中分离出浮点值度量单位(%,ms等),下面的代码可以得到我想要的结果,但我想知道是否有更简洁的方法来实现相同的逻辑,也许不需要使用正则表达式。

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {

	regexFloatNumbers := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`)

	values := [5]string{"32.5ms", "32.5 ms", "32.5%", "32.5 %", "none"}

	for _, value := range values {

		if regexFloatNumbers.MatchString(value) {
			floatValue := regexFloatNumbers.FindString(value)
			fmt.Printf("ORIGINAL VALUE: %q\n", value)
			fmt.Printf("UNIT: %q\n", strings.TrimSpace(regexFloatNumbers.Split(value, -1)[1]))
			fmt.Printf("FLOAT VALUE: %v\n\n", floatValue)
		} else {
			fmt.Printf("float value for %v has not being found!", value)
		}

	}

}
英文:

Having the following examples as possibles values from an index:

values := [5]string{"32.5ms", "32.5 ms", "32.5%", "32.5 %", "none"}

note that the original values could be with spaces, or without spaces from the measure unit (32.5%,32.5 %, 32.5 %,etc)

I need to split the float value and the unit of measure (%, ms, etc) from the original value, the bellow code, do the result I want, but I wanna know if there is some way more clean to do this same logic, maybe without regex.

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {

	regexFloatNumbers := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`)

	values := [5]string{"32.5ms", "32.5 ms", "32.5%", "32.5 %", "none"}

	for _, value := range values {

		if regexFloatNumbers.MatchString(value) {
			floatValue := regexFloatNumbers.FindString(value)
			fmt.Printf("ORIGINAL VALUE: %q\n", value)
			fmt.Printf("UNIT: %q\n", strings.TrimSpace(regexFloatNumbers.Split(value, -1)[1]))
			fmt.Printf("FLOAT VALUE: %v\n\n", floatValue)
		} else {
			fmt.Printf("float value for %v has not being found!", value)
		}

	}

}

答案1

得分: 1

正则表达式似乎是适合这里的工具。个人而言,我会使用子组来实现,就像这样(我还清理了你的正则表达式,其中有一些不必要的语法和看起来像是 [\d{2}]* 的拼写错误):

regexFloatNumbers := regexp.MustCompile(`(-?\d[\d,]*\.?\d*) *(.*)`)

// ...

floatValue := regexFloatNumbers.FindStringSubmatch(value)
fmt.Printf("原始值: %q\n", value)
fmt.Printf("单位: %q\n", floatValue[1])
fmt.Printf("浮点数值: %v\n\n", floatValue[2])

https://go.dev/play/p/S6Jig4V4OVs

英文:

Regular expressions seem like the right fit here. Personally I'd use subgroups for this, like so (I also cleaned up your regex a little which had some unnecessary syntax and what looks like a typo with [\d{2}]*):

regexFloatNumbers := regexp.MustCompile(`(-?\d[\d,]*\.?\d*) *(.*)`)

// ...

floatValue := regexFloatNumbers.FindStringSubmatch(value)
fmt.Printf("ORIGINAL VALUE: %q\n", value)
fmt.Printf("UNIT: %q\n", floatValue[1])
fmt.Printf("FLOAT VALUE: %v\n\n", floatValue[2])

https://go.dev/play/p/S6Jig4V4OVs

huangapple
  • 本文由 发表于 2022年12月7日 22:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/74718099.html
匿名

发表评论

匿名网友

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

确定