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

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

Golang removing all text between two specific strings

问题

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

  1. #start-kiwi
  2. 这段文本
  3. 很重要
  4. #end-kiwi
  5. #start-banana
  6. 这段文本
  7. 需要完全删除
  8. #end-banana
  9. #start-orange
  10. 这段文本
  11. 也很重要
  12. #end-orange

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

  1. #start-kiwi
  2. 这段文本
  3. 很重要
  4. #end-kiwi
  5. #start-orange
  6. 这段文本
  7. 也很重要
  8. #end-orange

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

  1. string.Contains(strings.Replace(input.txt, "#start-banana", "")
  2. 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:

  1. #start-kiwi
  2. this text
  3. is important
  4. #end-kiwi
  5. #start-banana
  6. this text
  7. needs to be
  8. completely removed
  9. #end-banana
  10. #start-orange
  11. this text
  12. is also important
  13. #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:

  1. #start-kiwi
  2. this text
  3. is important
  4. #end-kiwi
  5. #start-orange
  6. this text
  7. is also important
  8. #end-orange

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

  1. string.Contains(strings.Replace(input.txt, "#start-banana", "")
  2. 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

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. data := `
  8. #start-kiwi
  9. this text
  10. is important
  11. #end-kiwi
  12. #start-banana
  13. this text
  14. needs to be
  15. completely removed
  16. #end-banana
  17. #start-orange
  18. this text
  19. is also important
  20. #end-orange`
  21. start := "#start-banana"
  22. stop := "#end-banana"
  23. startIndex := strings.Index(data, start)
  24. stopIndex := strings.Index(data, stop) + len(stop)
  25. res := data[:startIndex] + data[stopIndex:]
  26. res = strings.ReplaceAll(res, "\n\n", "\n")
  27. fmt.Println(res)
  28. }

结果将会是以下内容:

  1. #start-kiwi
  2. this text
  3. is important
  4. #end-kiwi
  5. #start-orange
  6. this text
  7. is also important
  8. #end-orange
英文:

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. data := `
  8. #start-kiwi
  9. this text
  10. is important
  11. #end-kiwi
  12. #start-banana
  13. this text
  14. needs to be
  15. completely removed
  16. #end-banana
  17. #start-orange
  18. this text
  19. is also important
  20. #end-orange`
  21. start := "#start-banana"
  22. stop := "#end-banana"
  23. startIndex := strings.Index(data, start)
  24. stopIndex := strings.Index(data, stop) + len(stop)
  25. res := data[:startIndex] + data[stopIndex:]
  26. res = strings.ReplaceAll(res, "\n\n", "\n")
  27. fmt.Println(res)
  28. }

The result will be the following:

  1. #start-kiwi
  2. this text
  3. is important
  4. #end-kiwi
  5. #start-orange
  6. this text
  7. is also important
  8. #end-orange

答案2

得分: 0

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

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. s := removeBetween(text, "#start-banana", "#end-banana[\n\r]?")
  8. fmt.Println(s)
  9. }
  10. // removeBetween函数会删除开始和结束标记之间的所有字符(包括换行符)
  11. func removeBetween(str, start, end string) string {
  12. anyIncludingEndLine := fmt.Sprintf(`%s[\r\n\s\w]*%s`, start, end)
  13. return regexp.MustCompile(anyIncludingEndLine).ReplaceAllString(str, "")
  14. }
  15. var text = `
  16. #start-kiwi
  17. this text
  18. is important
  19. #end-kiwi
  20. #start-banana
  21. this text
  22. needs to be
  23. completely removed
  24. #end-banana
  25. #start-orange
  26. this text
  27. is also important
  28. #end-orange
  29. `
英文:

You can also use regular expressions:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. s := removeBetween(text, "#start-banana", "#end-banana[\n\r]?")
  8. fmt.Println(s)
  9. }
  10. // removeBetween removes all characters (including new lines) between the start and end markers
  11. func removeBetween(str, start, end string) string {
  12. anyIncludingEndLine := fmt.Sprintf(`%s[\r\n\s\w]*%s`, start, end)
  13. return regexp.MustCompile(anyIncludingEndLine).ReplaceAllString(str, "")
  14. }
  15. var text = `
  16. #start-kiwi
  17. this text
  18. is important
  19. #end-kiwi
  20. #start-banana
  21. this text
  22. needs to be
  23. completely removed
  24. #end-banana
  25. #start-orange
  26. this text
  27. is also important
  28. #end-orange
  29. `

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:

确定