Golang删除两个特定字符串之间的所有文本。

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

Golang removing all text between two specific strings

问题

从go/golang中是否可以删除两个字符串之间的所有内容?我有一个input.txt文件,其结构如下:

#start-kiwi
这段文本
很重要
#end-kiwi

#start-banana
这段文本
需要完全删除
#end-banana

#start-orange
这段文本
也很重要
#end-orange

我正在尝试从go代码中删除标记为#start-banana#end-banana之间的所有内容(包括两个标记),所以期望的结果应该是:

#start-kiwi
这段文本
很重要
#end-kiwi

#start-orange
这段文本
也很重要
#end-orange

我正在使用go 1.19,我已经尝试了以下方法:

string.Contains(strings.Replace(input.txt, "#start-banana", "")
string.Contains(strings.Replace(input.txt, "#end-banana", "")

但似乎并没有正常工作。有没有更好的方法来实现这个?正则表达式?使用strings库?

提前感谢。

英文:

Is it possible from go/golang to just delete everything contained in between two strings?
I have a input.txt file which has the following structure:

#start-kiwi
this text
is important
#end-kiwi

#start-banana
this text
needs to be
completely removed
#end-banana

#start-orange
this text
is also important
#end-orange

From go code I am trying to delete everything in between the markers #start-banana and #end-banana (included both) so the desired result would be:

#start-kiwi
this text
is important
#end-kiwi

#start-orange
this text
is also important
#end-orange

I am using go 1.19 and I have already tried these methods:

string.Contains(strings.Replace(input.txt, "#start-banana", "")
string.Contains(strings.Replace(input.txt, "#end-banana", "")

But it seems like it is not working all right. Is there any preferred method of achieving this? RegEx? With strings library?

Thanks in advance.

答案1

得分: 1

你可以使用索引来确定需要删除的文本部分:

package main

import (
	"fmt"
	"strings"
)

func main() {
	data := `
#start-kiwi
this text
is important
#end-kiwi

#start-banana
this text
needs to be
completely removed
#end-banana

#start-orange
this text
is also important
#end-orange`

	start := "#start-banana"
	stop := "#end-banana"
	startIndex := strings.Index(data, start)
	stopIndex := strings.Index(data, stop) + len(stop)
	res := data[:startIndex] + data[stopIndex:]
	res = strings.ReplaceAll(res, "\n\n", "\n")
	fmt.Println(res)
}

结果将会是以下内容:

#start-kiwi
this text
is important
#end-kiwi

#start-orange
this text
is also important
#end-orange
英文:

You can use the index in order to delimit the portion of text that have to be deleted:

package main

import (
	"fmt"
	"strings"
)

func main() {
	data := `
#start-kiwi
this text
is important
#end-kiwi

#start-banana
this text
needs to be
completely removed
#end-banana

#start-orange
this text
is also important
#end-orange`

	start := "#start-banana"
	stop := "#end-banana"
	startIndex := strings.Index(data, start)
	stopIndex := strings.Index(data, stop) + len(stop)
	res := data[:startIndex] + data[stopIndex:]
	res = strings.ReplaceAll(res, "\n\n", "\n")
	fmt.Println(res)
}

The result will be the following:

#start-kiwi
this text
is important
#end-kiwi

#start-orange
this text
is also important
#end-orange

答案2

得分: 0

你也可以使用正则表达式:

package main

import (
	"fmt"
	"regexp"
)

func main() {

	s := removeBetween(text, "#start-banana", "#end-banana[\n\r]?")

	fmt.Println(s)
}

// removeBetween函数会删除开始和结束标记之间的所有字符(包括换行符)
func removeBetween(str, start, end string) string {

	anyIncludingEndLine := fmt.Sprintf(`%s[\r\n\s\w]*%s`, start, end)

	return regexp.MustCompile(anyIncludingEndLine).ReplaceAllString(str, "")
}

var text = `
#start-kiwi
this text
is important
#end-kiwi

#start-banana
this text
needs to be
completely removed
#end-banana

#start-orange
this text
is also important
#end-orange
`
英文:

You can also use regular expressions:

package main

import (
	"fmt"
	"regexp"
)

func main() {

	s := removeBetween(text, "#start-banana", "#end-banana[\n\r]?")

	fmt.Println(s)
}

// removeBetween removes all characters (including new lines) between the start and end markers
func removeBetween(str, start, end string) string {

	anyIncludingEndLine := fmt.Sprintf(`%s[\r\n\s\w]*%s`, start, end)

	return regexp.MustCompile(anyIncludingEndLine).ReplaceAllString(str, "")
}

var text = `
#start-kiwi
this text
is important
#end-kiwi

#start-banana
this text
needs to be
completely removed
#end-banana

#start-orange
this text
is also important
#end-orange
`

huangapple
  • 本文由 发表于 2022年9月23日 15:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/73824519.html
匿名

发表评论

匿名网友

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

确定