I have a string that I am trying parse using go regexp and convert to os.Getenv(env)
. Assuming we have string "hello world -p $PATH -f $HOSTNAME "
in Go, and want to use regexp to read os.Getenv("PATH")
and os.Getenv("HOSTNAME")
. Anyone can help me with a proper regexp parser that will extract out "$"
along with the word pass it through the os.Getenv()
as the parameter for getting the environment variable?
The os.ExpandEnv function replaces $var or ${var} in a string according to the values in the environment. I recommend using this function instead of writing your own using regular expressions.