golang exec find命令正则表达式[重复]

This question already has an answer here:

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>

By default, find interprets regular expressions as Emacs Regular Expressions, where, particularly, the alteration operator is \|, and grouping is performed with backslashes followed by parentheses \(, \). So you should either use the correct Emacs Regular Expressions syntax, or use alternative regular expressions syntax with the help of -regextype option, where these operators are not prefixed with backslashes, e.g.:

find -regextype 'posix-extended' -regex '.*(type-a|type-b)/os.*'

Since backslash in Go is used as an escape character in strings, you should escape the backslash character itself with an extra backslash, e.g.:

cmd := exec.Command("/usr/bin/find", "/opt/system/versions",
                    "-mindepth",  "3", "-maxdepth",  "3",
                    "-regex",  ".*\\(type-a\\|type-b\\)\\/os.*")