How to remove lines that started with certain substring in []byte
, in Ruby
usually I do something like this:
lines = lines.split("\n").reject{|r| r.include? 'substring'}.join("\n")
How to do this on Go
?
How to remove lines that started with certain substring in []byte
, in Ruby
usually I do something like this:
lines = lines.split("\n").reject{|r| r.include? 'substring'}.join("\n")
How to do this on Go
?
You could emulate that with regexp:
re := regexp.MustCompile("(?m)[\r\n]+^.*substring.*$")
res := re.ReplaceAllString(s, "")
(The OP Kokizzu went with "(?m)^.*" +substr+ ".*$[\r\n]+"
)
See this example
func main() {
s := `aaaaa
bbbb
cc substring ddd
eeee
ffff`
re := regexp.MustCompile("(?m)[\r\n]+^.*substring.*$")
res := re.ReplaceAllString(s, "")
fmt.Println(res)
}
output:
aaaaa
bbbb
eeee
ffff
Note the use of regexp flag (?m):
multi-line mode:
^
and$
match begin/end line in addition to begin/end text (default false)
I believe using the bytes
package for this task is better than using regexp
.
package main
import (
"fmt"
"bytes"
)
func main() {
myString := []byte("aaaa\nsubstring\nbbbb")
lines := bytes.Replace(myString, []byte("substring\n"), []byte(""), 1)
fmt.Println(string(lines)) // Convert the bytes to string for printing
}
Output:
aaaa
bbbb
The question title does not have the same meaning as the way the original question was worded. The Regex provided as the accepted solution did not solve for the use case I had of removing a whole line when finding a matching substring, like the question title indicates.
In order to remove lines that contain certain substrings in Go (title of the question), you could implement something in Go very similar to the Ruby code that Kokizzu wrote initially.
func removeLinesContainingAny(input string, toRemove []string) string {
if !strings.HasSuffix(input, "\n") {
input += "\n"
}
lines := strings.Split(input, "\n")
for i, line := range lines {
for _, rm := range toRemove {
if strings.Contains(line, rm) {
lines = append(lines[:i], lines[i+1:]...)
}
}
}
input = strings.Join(lines, "\n")
input = strings.TrimSpace(input)
input += "\n"
return input
}
See test cases here: https://go.dev/play/p/K-biYIO1kjk
In particular, you need to ensure there is a new line at the end of the input string, otherwise you will get a panic for slice bounds out of range
in certain cases.
This approved solution will not work when you need to remove the top line :
func main() {
s := `aaaaa substring
bbbb
cc substring ddd
eeee
ffff`
re := regexp.MustCompile("(?m)[\r\n]+^.*substring.*$")`enter code here`
res := re.ReplaceAllString(s, "")
fmt.Println(res)
}