Home > Mobile >  Golang removing all text between two specific strings
Golang removing all text between two specific strings

Time:09-23

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.

CodePudding user response:

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

CodePudding user response:

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
`
  • Related