On 10/20/2015 01:49 PM, Oleg Trenkin wrote:
Hi all,
Does anybody know why I can't evaluate environment variable inside
"REGEX REPLACE"?
Example,
I have a value:
set(value "LD_LIBRARY_PATH=my_path:$LD_LIBRARY_PATH")
I expect:
value = "LD_LIBRARY_PATH=my_path:some_system_path_from_env"
What I'm doing:
string(REGEX REPLACE "\\$\\{([a-zA-Z0-9_]+)\\}" "$ENV{\\1}" value ${value})
Regexp works,
CMAKE_MATCH_1 and \\1 both have "LD_LIBRARY_PATH" string
string(REGEX REPLACE "\\$\\{([a-zA-Z0-9_]+)\\}" "\\1" value ${value})
will return "LD_LIBRARY_PATH=my_path:LD_LIBRARY_PATH"
but why can't I use $ENV{\\1}, or maybe I'm doing something wrong?
$ENV{} is evaluated before string() gets called so \\1 is evaluated as
the literal name of the variable.
This works for me somewhat:
set(value "LD_LIBRARY_PATH=my_path:$LD_LIBRARY_PATH")
string(REGEX MATCHALL "\\$[a-zA-Z0-9_]+" MY_MATCHES "${value}")
foreach(MATCH ${MY_MATCHES})
string(SUBSTRING "${MATCH}" 1 -1 MY_NAME)
string(REPLACE "${MATCH}" "$ENV{${MY_NAME}}" value "${value}")
endforeach()
message("${value}")
Nils
--
Powered by www.kitware.com
Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ
Kitware offers various services to support the CMake community. For more
information on each offering, please visit:
CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake