This question already has an answer here:
- “Unknown escape sequence” error in Go 2 answers
I am trying to write a find
command (shell command below) in go:
find . -mindepth 3 -maxdepth 3 -regex '.*\(type-a\|type-b\)\/os.*'
Here is the go snippet:
package main
import (
"bytes"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("/usr/bin/find", "/opt/system/versions",
"-mindepth", "3", "-maxdepth", "3",
"-regex", ".*(type-a|type-b)/os.*")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err:= cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return
}
fmt.Println("Directory contents : ", out.String())
}
It works fine if I search for just type-a
(or just type-b
). Does not work when I search for either type-a
or type-b
(the shell command works just fine). What did I get wrong in the regex pattern fed into Command
?
When I escape the (
, |
and )
using \
, go complains about the escape sequence - unknown escape sequence: (
:
cmd := exec.Command("/usr/bin/find", "/opt/system/versions",
"-mindepth", "3", "-maxdepth", "3",
"-regex", ".*\(type-a\|type-b\)/os.*")
</div>