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?

正则表达式将字符串解析为环境变量
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报