doumeng9188 2017-05-05 17:15
浏览 600

是否可以在Golang中的一次操作中提取字符串的一部分并替换这些部分?

Say I want to extract all numbers from a string (Most likely using regex matching) and I also want to replace those number matches with a generic placeholder like "#".

This is easily done in two parts using FindAll, then ReplaceAll. However I have serious doubts about the performance costs of doing such operations.

So take a string

"sdasd 3.2% sadas 6 ... +8.9"

replace it with

"sdasd #% sadas # ... +#"

and get a slice

[3.2,6.0,8.9]

In the most performant way possible.

Edit : I implemented the regexp.FindAllString + regexp.ReplaceAllString and the performance hit to my app was very minimal. I will hopefully try Elliot Chance's approach and compare the two when I have time.

  • 写回答

2条回答

  • doumi2106 2017-05-05 17:21
    关注

    If you need raw performance than regexp is rarely the way to achieve it, even if it is convenient. Iterating token by token should be pretty fast. Some code:

    input := "sdasd 3.2 sadas 6"
    output := []string{}
    numbers := []float64{}
    
    for _, tok := range strings.Split(input, " ") {
        if f, err := strconv.ParseFloat(tok, 64); err == nil {
            numbers = append(numbers, f)
            tok = "#"
        }
        output = append(output, tok)
    }
    finalString := strings.Join(output, " ")
    fmt.Println(finalString, numbers)
    

    playground link

    I'm sure there's a few more optimizations in there that could be made, but this is the general approach I'd take.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序