Go中是否有任何方法或具有正则表达式的方法仅会删除字符串中使用的文章?
我尝试了下面的代码来做到这一点,但它也会从字符串中删除其他单词,而我正在显示以下代码:
removalString := "This is a string"
stringToRemove := []string{"a", "an", "the", "is"}
for _, wordToRemove := range stringToRemove {
removalString = strings.Replace(removalString, wordToRemove, "", -1)
}
space := regexp.MustCompile(`\s+`)
trimedExtraSpaces := space.ReplaceAllString(removalString, " ")
spacesCovertedtoDashes := strings.Replace(trimedExtraSpaces, " ", "-", -1)
slug := strings.ToLower(spacesCovertedtoDashes)
fmt.Println(slug)
已编辑
在此操作中,它将删除is
中使用的this
。
预期输出为this-string
答案 0 :(得分:2)
您可以使用strings.Split
和strings.Join
加上一个循环进行过滤,然后再次将其构建在一起:
removalString := "This is a string"
stringToRemove := []string{"a", "an", "the", "is"}
filteredStrings := make([]string, 0)
for _, w := range strings.Split(removalString, " ") {
shouldAppend := true
lowered := strings.ToLower(w)
for _, w2 := range stringToRemove {
if lowered == w2 {
shouldAppend = false
break
}
}
if shouldAppend {
filteredStrings = append(filteredStrings, lowered)
}
}
resultString := strings.Join(filteredStrings, "-")
fmt.Printf(resultString)
输出:
this-string
Program exited.
这里有live example
答案 1 :(得分:1)
我的版本仅使用regexp
构造形式为'\ ba \ b | \ ban \ b | \ bthe \ b | \ bis \ b |'的正则表达式会发现 列表中的单词两边都有“单词边界”-因此“ This”不匹配
第二个正则表达式将所有空格减少为破折号,并使多个空格变为单个破折号
package main
import (
"bytes"
"fmt"
"regexp"
)
func main() {
removalString := "This is a strange string"
stringToRemove := []string{"a", "an", "the", "is"}
var reg bytes.Buffer
for _, x := range stringToRemove {
reg.WriteString(`\b`) // word boundary
reg.WriteString(x)
reg.WriteString(`\b`)
reg.WriteString(`|`) // alternation operator
}
regx := regexp.MustCompile(reg.String())
slug := regx.ReplaceAllString(removalString, "")
regx2 := regexp.MustCompile(` +`)
slug = regx2.ReplaceAllString(slug, "-")
fmt.Println(slug)
}