duanguanzai6181 2014-09-21 22:17
浏览 163
已采纳

正则表达式将字符串解析为环境变量

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?

  • 写回答

1条回答 默认 最新

  • doujingya1166 2014-09-21 22:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?