[CMake] string MATCHALL
Gregor Jasny
gjasny at googlemail.com
Tue Oct 24 16:52:43 EDT 2017
In case it helps: the GUI can contains a regex explorer where you can play
with rexexes. But beware: there are some corner cases around quoting
special things.
I'd suggest to use if( ... MATCHES ...) And read the match from
the CMAKE_MATCH_<n> variables.
On Oct 24, 2017 11:16, "Edoardo Pasca" <edo.paskino at gmail.com> wrote:
Dear all,
I'm trying to determine the active conda environment (name and path) at
cmake runtime.
To do that I issue the command
conda env list, which yields lines with the active environment highlighted
with an asterisk: something similar to this:
env1 /path/to/env1
env2 / path/to/env2
env3 * /path/to/env3
It is a perfect task for a match of REGEX but I cannot get it right and I
defaulted to using brute force to achieve my goal.
My brute force method is
1) splitting the lines by replacing \n with ;
2) now that I have a LIST (or I think that is a list) I can run a foreach
to see if line-by-line I match the following string(REGEX MATCHALL
"(.+)[*](.+)" match ${line})
Now, I'd expect to find in ${match} a list with the captured strings, i.e.
env3;/path/to/env3. However that's not the case ${match} contains the whole
line. So now I REPLACE "*" with ";" and now I have a almost LIST. Actually
I do this:
string(REPLACE "*" ";" ENV_DIR ${match})
list (APPEND cc "")
foreach(conda ${ENV_DIR})
string(STRIP ${conda} stripped)
list(APPEND cc ${stripped})
endforeach()
Finally I've got my list ${cc} with the name and path of the environment as
first and second element.
The question is, how would I use MATCHALL to achieve my goal (if possible)?
for the record I attach here the whole script.
Thanks for your help
Edo
execute_process(COMMAND "conda" "env" "list"
OUTPUT_VARIABLE _CONDA_ENVS
RESULT_VARIABLE _CONDA_RESULT
ERROR_VARIABLE _CONDA_ERR)
if(NOT _CONDA_RESULT)
string(REPLACE "\n" ";" ENV_LIST ${_CONDA_ENVS})
foreach(line ${ENV_LIST})
string(REGEX MATCHALL "(.+)[*](.+)" match ${line})
if (NOT ${match} EQUAL "")
string(REPLACE "*" ";" ENV_DIR ${match})
list (APPEND cc "")
foreach(conda ${ENV_DIR})
string(STRIP ${conda} stripped)
list(APPEND cc ${stripped})
endforeach()
list(LENGTH cc Ns)
if (${Ns} EQUAL 2)
list(GET cc 0 CONDA_ENVIRONMENT)
list(GET cc 1 CONDA_ENVIRONMENT_PATH)
endif()
endif()
endforeach()
else()
message(FATAL_ERROR "ERROR with conda command " ${_CONDA_ERR})
endif()
--
Edo
I know you think you understand what you thought I said, but I'm not sure
you realize that what you heard is not what I meant (prob. Alan Greenspan)
:wq
--
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/opensou
rce/opensource.html
Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20171024/72a2e7e5/attachment-0001.html>
More information about the CMake
mailing list