在golang中,如何删除两个字母之间的引号,如下所示:

import (
    "testing"
)

func TestRemoveQuotes(t *testing.T) {
    var a = "bus\"zipcode"
    var mockResult = "bus zipcode"
    a = RemoveQuotes(a)

    if a != mockResult {
        t.Error("Error or TestRemoveQuotes: ", a)
    }
}

功能:
import (
    "fmt"
    "strings"
)

func RemoveQuotes(s string) string {
    s = strings.Replace(s, "\"", "", -1) //here I removed all quotes. I'd like to remove only quotes between letters

    fmt.Println(s)

    return s
}

例如:

最佳答案

您可以使用简单的\b"\b正则表达式,仅在单词边界前后加上双引号时才匹配双引号:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    var a = "\"test1\",\"test2\",\"tes\"t3\""
    fmt.Println(RemoveQuotes(a))
}

func RemoveQuotes(s string) string {
    re := regexp.MustCompile(`\b"\b`)
    return re.ReplaceAllString(s, "")
}

请参阅Go demo打印"test1","test2","test3"

另外,请参见online regex demo

关于regex - 删除字母之间的引号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44376517/

10-11 10:48